benchmark.py 1.4 KB

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