Created
March 25, 2016 11:39
-
-
Save RaD/40a0d4094af95d1c3447 to your computer and use it in GitHub Desktop.
To remember dump question about mutable types.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
python who_faster.py | |
step 1 | |
check_list 5.89978313446 | |
check_set 0.0123991966248 | |
check_dict 0.063777923584 | |
step 2 | |
check_list 6.33801198006 | |
check_set 0.0101110935211 | |
check_dict 0.0368499755859 | |
step 3 | |
check_list 7.03751802444 | |
check_set 0.0100300312042 | |
check_dict 0.0443878173828 | |
step 4 | |
check_list 5.35815000534 | |
check_set 0.0166079998016 | |
check_dict 0.0494339466095 | |
step 5 | |
check_list 6.40436792374 | |
check_set 0.0137557983398 | |
check_dict 0.059180021286 | |
""" | |
import time | |
import functools | |
def timeit(f): | |
@functools.wraps(f) | |
def wrapper(*args, **kwargs): | |
value = time.time() | |
result = f(*args, **kwargs) | |
print f.__name__, time.time() - value | |
return result | |
return wrapper | |
@timeit | |
def check_list(limit=10000): | |
storage = list() | |
for i in xrange(limit): | |
if i not in storage: | |
storage.append(i) | |
@timeit | |
def check_set(limit=10000): | |
storage = set() | |
for i in xrange(limit): | |
if i not in storage: | |
storage.add(i) | |
@timeit | |
def check_dict(limit=10000): | |
storage = dict() | |
for i in xrange(limit): | |
if i not in storage: | |
storage.update({i: None}) | |
def check_all(limit=10000): | |
check_list(limit) | |
check_set(limit) | |
check_dict(limit) | |
for i in xrange(5): | |
print('step {}'.format(i+1)) | |
check_all(10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment