Last active
July 14, 2018 16:48
-
-
Save jan-matejka/2281361acdd2291b5b6b208c2b6e1bf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
setup = ''' | |
iter = 1 | |
#iter = 10**6 | |
class Attr: | |
def __init__(self): | |
self.x = 1 | |
self._range = range(iter) | |
def call(self): | |
[self.x for i in self._range] | |
class Local: | |
def __init__(self): | |
self.x = 1 | |
self._range = range(iter) | |
def call(self): | |
x = self.x | |
[x for i in self._range] | |
a = Attr() | |
l = Local() | |
''' | |
a = timeit.timeit('a.call()', setup, number=1) | |
l = timeit.timeit('l.call()', setup, number=1) | |
diff = a - l | |
print("attr: %f" % a) | |
print("local: %f" % l) | |
print("diff: %f" % diff) | |
# results on my workstation | |
# for iter=1 diff = between 0 and 3 microseconds | |
# usually 1 microsecond | |
# occasionally -0 and I saw -1 once | |
# for iter=10**6 diff = 6±1 miliseconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment