""" Micro-benchmark for the Python operation str.format(). Run it with: ./python.orig benchmark.py script bench_long.py --file=orig ./python.patched benchmark.py script bench_long.py --file=patch ./python.patched benchmark.py compare_to orig patch Download benchmark.py from: https://bitbucket.org/haypo/misc/raw/tip/python/benchmark.py """ import sys def run_benchmark(bench): x = 3 y = 2 for x_type, y_type in ( ('int', 'int'), ('int', 'float'), ('float', 'int'), ('float', 'float'), ('Decimal', 'Decimal'), ): bench.start_group("%s - %s" % (x_type, y_type)) for op in ('+', '-', '*', '**'): expr = 'x %s y; ' % op setup = [] if 'Decimal' in (x_type, y_type): setup.append('from decimal import Decimal') setup.append('x=%s(%s)' % (x_type, x)) setup.append('y=%s(%s)' % (y_type, y)) setup = '; '.join(setup) name = "%s(%s) %s %s(%s)" % (x_type, x, op, y_type, y) bench.timeit(expr, setup=setup, name=name) bench.timeit(expr * 100, setup=setup, name="%s ran 100 times" % name) bench.start_group("strings") bench.timeit("x + y", setup="x='abc'; y='def'", name="'abc' + 'def'") bench.timeit("x + y; " * 100, setup="x='abc'; y='def'", name="'abc' + 'def' ran 100 times") bench.timeit("x * y; ", setup="x='abc'; y=3", name="'abc' * 3") bench.timeit("x * y; " * 100, setup="x='abc'; y=3", name="'abc' * 3 ran 100 times")

AltStyle によって変換されたページ (->オリジナル) /