[cu2qu] Move benchmark code into cu2qu.benchmark module
This commit is contained in:
parent
2d5a3576d1
commit
80033bb2a0
48
Lib/fontTools/cu2qu/benchmark.py
Normal file
48
Lib/fontTools/cu2qu/benchmark.py
Normal file
@ -0,0 +1,48 @@
|
||||
"""Benchmark the cu2qu algorithm performance."""
|
||||
|
||||
from .cu2qu import *
|
||||
import random
|
||||
import timeit
|
||||
|
||||
MAX_ERR = 5
|
||||
|
||||
def generate_curve():
|
||||
return [
|
||||
tuple(float(random.randint(0, 2048)) for coord in range(2))
|
||||
for point in range(4)]
|
||||
|
||||
def setup_curve_to_quadratic():
|
||||
return generate_curve(), MAX_ERR
|
||||
|
||||
def setup_curves_to_quadratic():
|
||||
num_curves = 3
|
||||
return (
|
||||
[generate_curve() for curve in range(num_curves)],
|
||||
[MAX_ERR] * num_curves)
|
||||
|
||||
def run_benchmark(
|
||||
benchmark_module, module, function, setup_suffix='', repeat=5, number=1000):
|
||||
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. / number))
|
||||
|
||||
def main():
|
||||
run_benchmark('cu2qu.benchmark', 'cu2qu', 'curve_to_quadratic')
|
||||
run_benchmark('cu2qu.benchmark', 'cu2qu', 'curves_to_quadratic')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
random.seed(1)
|
||||
main()
|
@ -450,47 +450,3 @@ def curves_to_quadratic(curves, max_errors):
|
||||
raise ApproxNotFoundError(curves)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import random
|
||||
import timeit
|
||||
|
||||
MAX_ERR = 5
|
||||
|
||||
def generate_curve():
|
||||
return [
|
||||
tuple(float(random.randint(0, 2048)) for coord in range(2))
|
||||
for point in range(4)]
|
||||
|
||||
def setup_curve_to_quadratic():
|
||||
return generate_curve(), MAX_ERR
|
||||
|
||||
def setup_curves_to_quadratic():
|
||||
num_curves = 3
|
||||
return (
|
||||
[generate_curve() for curve in range(num_curves)],
|
||||
[MAX_ERR] * num_curves)
|
||||
|
||||
def run_benchmark(
|
||||
benchmark_module, module, function, setup_suffix='', repeat=5, number=1000):
|
||||
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. / number))
|
||||
|
||||
def main():
|
||||
run_benchmark('cu2qu.benchmark', 'cu2qu', 'curve_to_quadratic')
|
||||
run_benchmark('cu2qu.benchmark', 'cu2qu', 'curves_to_quadratic')
|
||||
|
||||
random.seed(1)
|
||||
main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user