1"""Benchmark the cu2qu algorithm performance.""" 2 3from .cu2qu import * 4import random 5import timeit 6 7MAX_ERR = 0.05 8 9 10def generate_curve(): 11 return [ 12 tuple(float(random.randint(0, 2048)) for coord in range(2)) 13 for point in range(4) 14 ] 15 16 17def setup_curve_to_quadratic(): 18 return generate_curve(), MAX_ERR 19 20 21def setup_curves_to_quadratic(): 22 num_curves = 3 23 return ([generate_curve() for curve in range(num_curves)], [MAX_ERR] * num_curves) 24 25 26def run_benchmark(module, function, setup_suffix="", repeat=5, number=1000): 27 setup_func = "setup_" + function 28 if setup_suffix: 29 print("%s with %s:" % (function, setup_suffix), end="") 30 setup_func += "_" + setup_suffix 31 else: 32 print("%s:" % function, end="") 33 34 def wrapper(function, setup_func): 35 function = globals()[function] 36 setup_func = globals()[setup_func] 37 38 def wrapped(): 39 return function(*setup_func()) 40 41 return wrapped 42 43 results = timeit.repeat(wrapper(function, setup_func), repeat=repeat, number=number) 44 print("\t%5.1fus" % (min(results) * 1000000.0 / number)) 45 46 47def main(): 48 """Benchmark the cu2qu algorithm performance.""" 49 run_benchmark("cu2qu", "curve_to_quadratic") 50 run_benchmark("cu2qu", "curves_to_quadratic") 51 52 53if __name__ == "__main__": 54 random.seed(1) 55 main() 56