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