fonttools/Lib/cu2qu/benchmark.py

74 lines
2.0 KiB
Python
Raw Normal View History

2015-12-01 17:28:19 -08:00
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function, division, absolute_import
import random
import timeit
MAX_ERR = 5
SETUP_CODE = '''
2016-07-27 16:50:57 -07:00
from %(module)s import %(function)s
from %(benchmark_module)s import %(setup_function)s
args = %(setup_function)s()
'''
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():
2016-04-20 14:40:49 -07:00
return generate_curve(), MAX_ERR
def setup_curves_to_quadratic():
num_curves = 3
return (
[generate_curve() for curve in range(num_curves)],
2016-04-20 14:40:49 -07:00
[MAX_ERR] * num_curves)
2016-07-27 16:50:57 -07:00
def run_benchmark(
benchmark_module, module, function, setup_suffix='', repeat=1000):
setup_func = 'setup_' + function
if setup_suffix:
print('%s with %s:' % (function, setup_suffix))
setup_func += '_' + setup_suffix
else:
print('%s:' % function)
results = timeit.repeat(
2016-07-27 16:50:57 -07:00
'%s(*args)' % function,
setup=(SETUP_CODE % {
'benchmark_module': benchmark_module, 'setup_function': setup_func,
'module': module, 'function': function}),
repeat=repeat, number=1)
print('min: %dus' % (min(results) * 1000000.))
print('avg: %dus' % (sum(results) / len(results) * 1000000.))
print()
def main():
2016-07-27 16:50:57 -07:00
run_benchmark('cu2qu.benchmark', 'cu2qu', 'curve_to_quadratic')
run_benchmark('cu2qu.benchmark', 'cu2qu', 'curves_to_quadratic')
if __name__ == '__main__':
random.seed(1)
main()