Skip to content

Instantly share code, notes, and snippets.

@markfickett
Created August 15, 2017 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markfickett/e092164ae3d88fbfba7c6f93ab9a5710 to your computer and use it in GitHub Desktop.
Save markfickett/e092164ae3d88fbfba7c6f93ab9a5710 to your computer and use it in GitHub Desktop.
Multi-threaded comparison of lookups in Python dict and ResizingList
from pyprofiling import Profiled
import logging
import random
import threading
class ResizingList(list):
"""A list of values that automatically grows when items are set, and returns None if an index
greater than the list length is provided."""
def __setitem__(self, index, value):
if index >= len(self):
self.extend([None] * (index + 1 - len(self)))
list.__setitem__(self, index, value)
def __getitem__(self, index):
if index >= len(self):
return None
return list.__getitem__(self, index)
logging.basicConfig(
format='%(levelname)s %(asctime)s %(filename)s:%(lineno)s: %(message)s',
level=logging.INFO)
available_ids = []
rl = ResizingList()
dictionary = {}
print('Setting up.')
for _ in xrange(900):
entity_id = random.randint(9999, 99999)
available_ids.append(entity_id)
rl[entity_id] = str(entity_id)
dictionary[entity_id] = str(entity_id)
class ProfilingThread(threading.Thread):
def run(self):
with Profiled('Look up 5k times in %d' % self.ident):
for _ in xrange(5000):
entity_id = random.choice(available_ids)
with Profiled('Look up in dictionary in %d' % self.ident):
v = dictionary[entity_id]
with Profiled('Look up in ResizingList in %d' % self.ident):
v = rl[entity_id]
t1 = ProfilingThread()
t2 = ProfilingThread()
t1.start()
t2.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment