Created
August 15, 2017 19:51
-
-
Save markfickett/c37e085fed88d321b773387f09012c56 to your computer and use it in GitHub Desktop.
Comparison of looking up Python class in dict
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
from pyprofiling import Profiled | |
import logging | |
import random | |
import threading | |
A_HASH_INDEX = 1 | |
class A(object): | |
pass | |
logging.basicConfig( | |
format='%(levelname)s %(asctime)s %(filename)s:%(lineno)s: %(message)s', | |
level=logging.INFO) | |
cache = {} | |
cache[A_HASH_INDEX] = A | |
cache[hash(A)] = A | |
cache[A.__name__] = A | |
with Profiled('Look up 5k times'): | |
for _ in xrange(5000): | |
with Profiled('Look up by static ID'): | |
v = cache[A_HASH_INDEX] | |
with Profiled('Look up by hash()'): | |
v = cache[hash(A)] | |
with Profiled('Look up by __name__'): | |
v = cache[A.__name__] | |
""" | |
mwf@mwf02:/tmp/compare$ python compare.py | |
INFO 2017-08-15 15:48:19,578 __init__.py:53: | |
Look up 5k times 1 * 0.071s = 0.071s | |
Look up by static ID 5000 * 0.000s = 0.004s (max 0.00s) | |
Look up by hash() 5000 * 0.000s = 0.005s | |
Look up by __name__ 5000 * 0.000s = 0.005s (max 0.00s) | |
remainder 0.06s | |
mwf@mwf02:/tmp/compare$ python compare.py | |
INFO 2017-08-15 15:48:20,501 __init__.py:53: | |
Look up 5k times 1 * 0.053s = 0.053s | |
Look up by static ID 5000 * 0.000s = 0.003s | |
Look up by hash() 5000 * 0.000s = 0.004s (max 0.00s) | |
Look up by __name__ 5000 * 0.000s = 0.004s (max 0.00s) | |
remainder 0.04s | |
mwf@mwf02:/tmp/compare$ python compare.py | |
INFO 2017-08-15 15:48:21,875 __init__.py:53: | |
Look up 5k times 1 * 0.050s = 0.050s | |
Look up by static ID 5000 * 0.000s = 0.003s | |
Look up by hash() 5000 * 0.000s = 0.004s | |
Look up by __name__ 5000 * 0.000s = 0.003s | |
remainder 0.04s | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment