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