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.
|
|
|
|
|
|
|
|
|
2015-12-01 17:13:01 -08:00
|
|
|
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()
|
2015-12-01 17:13:01 -08:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2015-12-01 17:13:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2015-12-01 17:13:01 -08:00
|
|
|
|
|
|
|
|
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:
|
2016-07-28 16:12:00 -07:00
|
|
|
print('%s with %s:' % (function, setup_suffix), end='')
|
2016-07-27 16:50:57 -07:00
|
|
|
setup_func += '_' + setup_suffix
|
|
|
|
else:
|
2016-07-28 14:18:33 -07:00
|
|
|
print('%s:' % function, end='')
|
2015-12-01 17:13:01 -08:00
|
|
|
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)
|
2016-07-28 14:18:33 -07:00
|
|
|
print('\tavg=%dus' % (sum(results) / len(results) * 1000000.),
|
|
|
|
'\tmin=%dus' % (min(results) * 1000000.))
|
2015-12-01 17:13:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-10-31 11:23:06 +00:00
|
|
|
run_benchmark('benchmark', 'cu2qu', 'curve_to_quadratic')
|
|
|
|
run_benchmark('benchmark', 'cu2qu', 'curves_to_quadratic')
|
2015-12-01 17:13:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-04-30 17:59:50 +02:00
|
|
|
random.seed(1)
|
2015-12-01 17:13:01 -08:00
|
|
|
main()
|