fonttools/Lib/fontTools/cu2qu/benchmark.py

58 lines
1.4 KiB
Python
Raw Normal View History

"""Benchmark the cu2qu algorithm performance."""
from .cu2qu import *
import random
import timeit
MAX_ERR = 5
2022-12-13 11:26:36 +00:00
def generate_curve():
return [
tuple(float(random.randint(0, 2048)) for coord in range(2))
2022-12-13 11:26:36 +00:00
for point in range(4)
]
def setup_curve_to_quadratic():
return generate_curve(), MAX_ERR
2022-12-13 11:26:36 +00:00
def setup_curves_to_quadratic():
num_curves = 3
2022-12-13 11:26:36 +00:00
return ([generate_curve() for curve in range(num_curves)], [MAX_ERR] * num_curves)
def run_benchmark(
2022-12-13 11:26:36 +00:00
benchmark_module, module, function, setup_suffix="", repeat=5, number=1000
):
setup_func = "setup_" + function
if setup_suffix:
2022-12-13 11:26:36 +00:00
print("%s with %s:" % (function, setup_suffix), end="")
setup_func += "_" + setup_suffix
else:
2022-12-13 11:26:36 +00:00
print("%s:" % function, end="")
def wrapper(function, setup_func):
function = globals()[function]
setup_func = globals()[setup_func]
2022-12-13 11:26:36 +00:00
def wrapped():
return function(*setup_func())
2022-12-13 11:26:36 +00:00
return wrapped
2022-12-13 11:26:36 +00:00
results = timeit.repeat(wrapper(function, setup_func), repeat=repeat, number=number)
2022-12-13 11:26:36 +00:00
print("\t%5.1fus" % (min(results) * 1000000.0 / number))
def main():
2022-08-25 14:38:35 -06:00
"""Benchmark the cu2qu algorithm performance."""
2022-12-13 11:26:36 +00:00
run_benchmark("cu2qu.benchmark", "cu2qu", "curve_to_quadratic")
run_benchmark("cu2qu.benchmark", "cu2qu", "curves_to_quadratic")
2022-12-13 11:26:36 +00:00
if __name__ == "__main__":
random.seed(1)
main()