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