""" Micro-benchmark for the Python fast call. Run it with: ./python.orig benchmark.py script bench_fast.py --file=orig ./python.patched benchmark.py script bench_fast.py --file=patched ./python.patched benchmark.py compare_to orig patched Download benchmark.py from: https://bitbucket.org/haypo/misc/raw/tip/python/benchmark.py """ def run_benchmark(bench): bench.timeit("list(filter(f, s))", "f = lambda x: x; s = list(range(1000))", "filter") bench.timeit("list(map(f, s))", "f = lambda x: x; s = list(range(1000))", "map") bench.timeit("sorted(s, key=f)", "f = lambda x: x; s = list(range(1000))", "sorted(list, key=lambda x: x)") bench.timeit("sorted(s)", "s = list(range(1000))", "sorted(list)") bench.timeit('bytes(b)', 'class MyBytes:\n def __bytes__(self): return b"abc"\nb = MyBytes()', 'b=MyBytes(); bytes(b)') bench.timeit("a.a; " * 20, "from collections import namedtuple as n; a = n('n', 'a b c')(1, 2, 3)", "namedtuple.attr") bench.timeit('set(obj, "x", 1)', 'class SimpleNamespace(object): pass\nset=object.__setattr__; obj=SimpleNamespace()', 'object.__setattr__(obj, "x", 1)') bench.timeit('get(obj, "x")', 'class SimpleNamespace(object): pass\nget=object.__getattribute__; obj=SimpleNamespace(); obj.x=1', 'object.__getattribute__(obj, "x")') bench.timeit('getattr(1, "real")') bench.timeit('m(1, 2)', 'class A:\n def meth(self, arg1, arg2): pass\nm=A().meth', 'bounded_pymethod(1, 2)') bench.timeit('m(a, 1, 2)', 'class A:\n def meth(self, arg1, arg2): pass\na=A(); m=A.meth', 'unbound_pymethod(obj, 1, 2)') bench.timeit('func()', 'def func(): pass', 'func()') bench.timeit('func(1, 2, 3)', 'def func(a, b, c): pass', 'func(1, 2, 3)')