Last active
February 12, 2020 21:12
-
-
Save loisaidasam/9c8fb17e915d20676445af34b91d1d6e to your computer and use it in GitHub Desktop.
Python: setattr() vs. direct object manipulation
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
$ python test_setattr.py | |
test_setattr 0.11335835400000001 | |
test_direct_object_manipulation 0.04550438199999998 | |
$ python test_setattr_data_types.py | |
int | |
setattr() 0.104297199 | |
direct obj manipulation 0.046985358000000005 | |
float | |
setattr() 0.097025698 | |
direct obj manipulation 0.04825673800000002 | |
str | |
setattr() 0.09857281400000001 | |
direct obj manipulation 0.04611676800000003 | |
dict | |
setattr() 0.12178510400000003 | |
direct obj manipulation 0.06695936300000005 | |
list | |
setattr() 0.10662076100000006 | |
direct obj manipulation 0.056601951999999955 | |
$ python test_getattr_data_types.py | |
int | |
getattr() 0.085680231 | |
direct obj manipulation 0.031511027999999996 | |
float | |
getattr() 0.079186949 | |
direct obj manipulation 0.03169878300000001 | |
str | |
getattr() 0.07892938700000002 | |
direct obj manipulation 0.03222381699999999 | |
dict | |
getattr() 0.07942887300000001 | |
direct obj manipulation 0.031215796000000018 | |
list | |
getattr() 0.07849608599999996 | |
direct obj manipulation 0.032060817999999935 |
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_str = """ | |
class A(object): | |
pass | |
a = A() | |
a.int = 5 | |
a.float = 0.5 | |
a.str = 'bar' | |
a.dict = {'foo': 'bar'} | |
a.list = ['a', 'b', 'c'] | |
""" | |
equivalent_functions = { | |
'int': ("getattr(a, 'int')", "a.int"), | |
'float': ("getattr(a, 'float')", "a.float"), | |
'str': ("getattr(a, 'str')", "a.str"), | |
'dict': ("getattr(a, 'dict')", "a.dict"), | |
'list': ("getattr(a, 'list')", "a.list"), | |
} | |
def main(): | |
for data_type, functions in equivalent_functions.items(): | |
print(data_type) | |
print("getattr()", timeit.timeit(functions[0], setup_str)) | |
print("direct obj manipulation", timeit.timeit(functions[1], setup_str)) | |
main() |
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_str = """ | |
class A(object): | |
pass | |
a = A() | |
""" | |
def test_setattr(): | |
return timeit.timeit("setattr(a, 'foo', 'bar')", setup=setup_str) | |
def test_direct_object_manipulation(): | |
return timeit.timeit("a.foo = 'bar'", setup=setup_str) | |
def main(): | |
print("test_setattr", test_setattr()) | |
print("test_direct_object_manipulation", test_direct_object_manipulation()) | |
main() |
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_str = """ | |
class A(object): | |
pass | |
a = A() | |
""" | |
equivalent_functions = { | |
'int': ("setattr(a, 'foo', 5)", "a.foo = 5"), | |
'float': ("setattr(a, 'foo', 0.5)", "a.foo = 0.5"), | |
'str': ("setattr(a, 'foo', 'bar')", "a.foo = 'bar'"), | |
'dict': ("setattr(a, 'foo', {})", "a.foo = {}"), | |
'list': ("setattr(a, 'foo', [])", "a.foo = []"), | |
} | |
def main(): | |
for data_type, functions in equivalent_functions.items(): | |
print(data_type) | |
print("setattr()", timeit.timeit(functions[0], setup_str)) | |
print("direct obj manipulation", timeit.timeit(functions[1], setup_str)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment