Skip to content

Instantly share code, notes, and snippets.

@hynek

hynek/bench.py Secret

Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hynek/056e0d206bc474af4b94 to your computer and use it in GitHub Desktop.
Save hynek/056e0d206bc474af4b94 to your computer and use it in GitHub Desktop.
from __future__ import absolute_import, division, print_function
from characteristic import attributes
@attributes(["a", "b", "c"])
class NoDefaults(object):
pass
@attributes(["a", "b", "c"], defaults={"c": 42})
class Defaults(object):
pass
def bench_no_defaults():
NoDefaults(a=1, b=2, c=3)
def bench_defaults():
Defaults(a=1, b=2)
def bench_both():
NoDefaults(a=1, b=2, c=3)
Defaults(a=1, b=2)
if __name__ == "__main__":
import timeit
for func in ["bench_no_defaults", "bench_defaults", "bench_both"]:
print(
func + ": ",
timeit.timeit(func + "()",
setup="from __main__ import {}".format(func))
)

Master branch

CPython 2.7

bench_no_defaults:  1.62143707275
bench_defaults:  2.23934602737
bench_both:  7.54101610184

CPython 3.4

bench_no_defaults:  3.4548427239933517
bench_defaults:  1.8643788420013152
bench_both:  3.678205877993605

PyPy

bench_no_defaults:  0.683818817139
bench_defaults:  0.537728071213
bench_both:  1.19528508186

Speedup branch

CPython 2.7

bench_no_defaults:  1.22634387016
bench_defaults:  1.16171097755
bench_both:  2.32142710686

CPython 3.4

bench_no_defaults:  1.4939782959991135
bench_defaults:  1.490283091996389
bench_both:  2.8763164740012144

PyPy

bench_no_defaults:  0.493278026581
bench_defaults:  0.389197826385
bench_both:  0.917297124863
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment