Commit c3d28872 authored by David A.. Werner's avatar David A.. Werner
Browse files

Added script to determine speedup of multi-threading

parent 73534ae1
""" This is a script that compares run-times of single-threaded vs multi-threaded simulation """
import time
import numpy as np
import multiprocessing
from functools import wraps
from manduca import SimpleManduca, EvolutionSimulator
def timed_function(return_result=True,return_time=True, print_time=False):
def timed_function_decorator(fn):
@wraps(fn)
def timed(*args, **kwargs):
start_time = time.time()
result = fn(*args, **kwargs)
end_time = time.time()
time_ms = (end_time-start_time) * 1000
if print_time:
print "{} {:2.2f} ms".format(fn.__name__, time_ms)
if return_result:
if return_time:
return result, time_ms
else:
return result
elif return_time:
return time_ms
return
return timed
return timed_function_decorator
@timed_function(return_result=False)
def run_sim():
return simulator.run_simulation(NUM_GENERATIONS)
POPULATION_SIZE, NUM_GENERATIONS= 10, 10
num_legs, time_segments, time_step = 5, 10, 10
def random_manduca():
return SimpleManduca.random_individual(num_legs, time_segments, time_step)
simulator = EvolutionSimulator((random_manduca, POPULATION_SIZE))
print "Running single-threaded code.."
time_ms = run_sim()
try:
pool = multiprocessing.Pool(int(multiprocessing.cpu_count()))
simulator = EvolutionSimulator((random_manduca, POPULATION_SIZE), pool=pool)
print "Running multi-threaded code.."
time_ms_multi = run_sim()
finally:
pool.close()
pool.join()
print "Number of cores: {}".format(multiprocessing.cpu_count())
print "Speedup: {}".format(time_ms / time_ms_multi)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment