From f75391f461af74cda09e3281e08772eb131fd009 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 16 Feb 2023 17:08:24 -0700 Subject: [PATCH] [qu2cu] Add .benchmark module --- Lib/fontTools/qu2cu/benchmark.py | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Lib/fontTools/qu2cu/benchmark.py diff --git a/Lib/fontTools/qu2cu/benchmark.py b/Lib/fontTools/qu2cu/benchmark.py new file mode 100644 index 000000000..3c9a1c693 --- /dev/null +++ b/Lib/fontTools/qu2cu/benchmark.py @@ -0,0 +1,52 @@ +"""Benchmark the qu2cu algorithm performance.""" + +from .qu2cu import * +from fontTools.cu2qu import curve_to_quadratic +import random +import timeit + +MAX_ERR = 0.05 + + +def generate_curve(): + return [ + tuple(float(random.randint(0, 2048)) for coord in range(2)) + for point in range(4) + ] + + +def setup_quadratic_to_curves(): + curve = generate_curve() + quadratics = curve_to_quadratic(curve, MAX_ERR) + return quadratics, MAX_ERR + + +def run_benchmark(module, function, setup_suffix="", repeat=5, number=10): + setup_func = "setup_" + function + if setup_suffix: + print("%s with %s:" % (function, setup_suffix), end="") + setup_func += "_" + setup_suffix + else: + print("%s:" % function, end="") + + def wrapper(function, setup_func): + function = globals()[function] + setup_func = globals()[setup_func] + + def wrapped(): + return function(*setup_func()) + + return wrapped + + results = timeit.repeat(wrapper(function, setup_func), repeat=repeat, number=number) + print("\t%5.1fus" % (min(results) * 1000000.0 / number)) + + +def main(): + """Benchmark the qu2cu algorithm performance.""" + run_benchmark("qu2cu", "quadratic_to_curves") + + +if __name__ == "__main__": + random.seed(1) + main()