Last active
November 7, 2017 14:55
-
-
Save yohanboniface/1b88461e348cf336820d23dedf654299 to your computer and use it in GitHub Desktop.
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
from timeit import timeit | |
class A: | |
def __init__(self): | |
self.kwargs = {} | |
class B(dict): | |
pass | |
class C: | |
def __init__(self): | |
self.kwargs = {} | |
def __setitem__(self, key, value): | |
self.kwargs[key] = value | |
def __getitem__(self, key): | |
return self.kwargs.get(key) | |
if __name__ == '__main__': | |
time = timeit('A()', number=100000, globals=globals()) | |
print(f'A init: {time}') | |
time = timeit('B()', number=100000, globals=globals()) | |
print(f'B init: {time}') | |
time = timeit('C()', number=100000, globals=globals()) | |
print(f'C init: {time}') | |
time = timeit('{}', number=100000, globals=globals()) | |
print(f'D init: {time}') | |
a = A() | |
time = timeit('a.kwargs["foo"] = "bar"', number=100000, globals=globals()) | |
print(f'A setitem: {time}') | |
b = B() | |
time = timeit('b["foo"] = "bar"', number=100000, globals=globals()) | |
print(f'B setitem: {time}') | |
c = C() | |
time = timeit('c["foo"] = "bar"', number=100000, globals=globals()) | |
print(f'C setitem: {time}') | |
d = {} | |
time = timeit('d["foo"] = "bar"', number=100000, globals=globals()) | |
print(f'D setitem: {time}') | |
a = A() | |
a.kwargs['foo'] = "bar" | |
time = timeit('bar = a.kwargs["foo"]', number=100000, globals=globals()) | |
print(f'A getitem: {time}') | |
b = B() | |
b['foo'] = "bar" | |
time = timeit('bar = b["foo"]', number=100000, globals=globals()) | |
print(f'B getitem: {time}') | |
c = C() | |
c['foo'] = "bar" | |
time = timeit('bar = c["foo"]', number=100000, globals=globals()) | |
print(f'C getitem: {time}') | |
d = {} | |
d['foo'] = "bar" | |
time = timeit('bar = d["foo"]', number=100000, globals=globals()) | |
print(f'D getitem: {time}') | |
# A init: 0.05380515399156138 | |
# B init: 0.026071301006595604 | |
# C init: 0.08211862100870349 | |
# D init: 0.007060791002004407 | |
# A setitem: 0.014063403010368347 | |
# B setitem: 0.00883179999073036 | |
# C setitem: 0.07186220100265928 | |
# D setitem: 0.01060778700048104 | |
# A getitem: 0.014394188998267055 | |
# B getitem: 0.02958642499288544 | |
# C getitem: 0.0863965710013872 | |
# D getitem: 0.008987113993498497 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment