Skip to content

Instantly share code, notes, and snippets.

@jedie
Created August 25, 2015 14:16
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 jedie/015d3cb9de25eb7f0188 to your computer and use it in GitHub Desktop.
Save jedie/015d3cb9de25eb7f0188 to your computer and use it in GitHub Desktop.
3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)]
test_normal: 3.264sec
test_short: 2.431sec
test_normal: 20000803 function calls in 5.011 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 5.011 5.011 <string>:1(<module>)
200 0.000 0.000 0.000 0.000 test.py:20(__init__)
10000000 0.918 0.000 0.918 0.000 test.py:23(set_one)
200 0.000 0.000 0.000 0.000 test.py:27(__init__)
200 0.000 0.000 0.000 0.000 test.py:32(__init__)
10000000 2.371 0.000 3.290 0.000 test.py:35(test)
200 1.721 0.009 5.011 0.025 test.py:50(test_normal)
1 0.000 0.000 5.011 5.011 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
test_short: 20000803 function calls in 4.277 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 4.277 4.277 <string>:1(<module>)
200 0.000 0.000 0.000 0.000 test.py:20(__init__)
10000000 0.905 0.000 0.905 0.000 test.py:23(set_one)
200 0.000 0.000 0.000 0.000 test.py:27(__init__)
200 0.000 0.000 0.000 0.000 test.py:40(__init__)
10000000 1.776 0.000 2.680 0.000 test.py:43(test)
200 1.596 0.008 4.276 0.021 test.py:58(test_short)
1 0.000 0.000 4.277 4.277 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
#!/usr/bin/env python3
from __future__ import absolute_import, division, print_function
from timeit import Timer
import cProfile
import sys
print(sys.version)
if sys.version_info[0] == 2:
range = xrange
INTERNAL_LOOPS = 50000
class Foo(object):
def __init__(self):
self.foo = 0
def set_one(self):
self.foo = 1
class Bar(object):
def __init__(self, foo):
self.foo = foo
class NormalExample(object):
def __init__(self, bar):
self.bar = bar
def test(self):
self.bar.foo.set_one()
class ShortExample(object):
def __init__(self, bar):
self.set_one = bar.foo.set_one
def test(self):
self.set_one()
#-----------------------------------------------------------------------------
def test_normal():
foo = Foo()
bar = Bar(foo)
x = NormalExample(bar)
for __ in range(INTERNAL_LOOPS):
x.test()
def test_short():
foo = Foo()
bar = Bar(foo)
x = ShortExample(bar)
for __ in range(INTERNAL_LOOPS):
x.test()
if __name__ == "__main__":
def time_it(func, number):
name = func.__name__
sys.stdout.write("%20s: " % name)
sys.stdout.flush()
t = Timer("%s()" % name, setup="from __main__ import %s" % name)
print("%.3fsec" % t.timeit(number))
def profile_it(func, number):
name = func.__name__
sys.stdout.write("%20s:" % name)
sys.stdout.flush()
cProfile.run(
'for __ in range(%(number)i):%(name)s()' % {
"name":name,
"number": number,
}
)
number = 200
time_it(test_normal, number)
time_it(test_short, number)
profile_it(test_normal, number)
profile_it(test_short, number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment