Created
July 9, 2015 16:00
-
-
Save thomas-mangin/f3ba31e1bad1a358be73 to your computer and use it in GitHub Desktop.
staticmethod vs classmethod
This file contains 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 | |
def nop_function (): | |
pass | |
def access_function (): | |
local = Test.scoped_value | |
class Test (object): | |
scoped_value = 1 | |
@staticmethod | |
def nop_class_static (): | |
pass | |
@classmethod | |
def nop_class_method (cls): | |
pass | |
def nop_class_function (self): | |
pass | |
# | |
@classmethod | |
def access_class_method (cls): | |
local = cls.scoped_value | |
def access_class_function (self): | |
local = self.scoped_value | |
def timer (function,loop=10000000): | |
start = time.time() | |
for _ in range(loop): | |
function() | |
end = time.time() | |
print 'function %s took %f' % (function.__name__,end-start) | |
test = Test() | |
# class creation time | |
timer(Test) | |
# class access time | |
timer(nop_function) | |
timer(Test.nop_class_static) | |
timer(Test.nop_class_method) | |
timer(test.nop_class_function) | |
timer(access_function) | |
timer(Test.access_class_method) | |
timer(test.access_class_function) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes, you just count create class time once. but if there is a need to create class instance each time, then classmethod is better, yes?