Created
September 12, 2015 10:17
-
-
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
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 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