benchmark.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Benchmark the cu2qu algorithm performance."""
  2. from .cu2qu import *
  3. import random
  4. import timeit
  5. MAX_ERR = 0.05
  6. def generate_curve():
  7. return [
  8. tuple(float(random.randint(0, 2048)) for coord in range(2))
  9. for point in range(4)
  10. ]
  11. def setup_curve_to_quadratic():
  12. return generate_curve(), MAX_ERR
  13. def setup_curves_to_quadratic():
  14. num_curves = 3
  15. return ([generate_curve() for curve in range(num_curves)], [MAX_ERR] * num_curves)
  16. def run_benchmark(module, function, setup_suffix="", repeat=5, number=1000):
  17. setup_func = "setup_" + function
  18. if setup_suffix:
  19. print("%s with %s:" % (function, setup_suffix), end="")
  20. setup_func += "_" + setup_suffix
  21. else:
  22. print("%s:" % function, end="")
  23. def wrapper(function, setup_func):
  24. function = globals()[function]
  25. setup_func = globals()[setup_func]
  26. def wrapped():
  27. return function(*setup_func())
  28. return wrapped
  29. results = timeit.repeat(wrapper(function, setup_func), repeat=repeat, number=number)
  30. print("\t%5.1fus" % (min(results) * 1000000.0 / number))
  31. def main():
  32. """Benchmark the cu2qu algorithm performance."""
  33. run_benchmark("cu2qu", "curve_to_quadratic")
  34. run_benchmark("cu2qu", "curves_to_quadratic")
  35. if __name__ == "__main__":
  36. random.seed(1)
  37. main()