Skip to content

Instantly share code, notes, and snippets.

@ceremcem
Created September 12, 2015 10:17
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 ceremcem/db8b1bf5b36578270647 to your computer and use it in GitHub Desktop.
Save ceremcem/db8b1bf5b36578270647 to your computer and use it in GitHub Desktop.
Performance comparison between iteration over list of classes and list of class methods
import time
class a:
def do_something(self, x):
return x**2
def do_something1(self, x):
return x**2
def do_something2(self, x):
return x**2
def do_something3(self, x):
return x**2
def do_something4(self, x):
return x**2
def do_something5(self, x):
return x**2
def do_something6(self, x):
return x**2
def do_something7(self, x):
return x**2
def do_something8(self, x):
return x**2
def do_something9(self, x):
return x**2
def do_something10(self, x):
return x**2
def do_something11(self, x):
return x**2
def do_something12(self, x):
return x**2
try_class = True
try_callback = False if try_class else True
if try_class:
li = [a() for i in range(100)]
if try_callback:
li = [a().do_something for i in range(100)]
start = time.time()
for k in range(400000):
for i in li:
if try_class:
i.do_something(5)
if try_callback:
i(5)
print "total: ", time.time() - start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment