Skip to content

Instantly share code, notes, and snippets.

@Torxed
Created March 11, 2016 16:09
Show Gist options
  • Save Torxed/b3872299c5659ea0ea09 to your computer and use it in GitHub Desktop.
Save Torxed/b3872299c5659ea0ea09 to your computer and use it in GitHub Desktop.
Short timing example between dict, list and set in python.
from time import time
from random import random
# Without checking the dictionary
x = {}
s = time()
for i in range(10000000):
x[round(random(), 6)] = True
e = time()
print(e-s, len(x), 'Using dict() for inserts')
s = time()
for i in range(10000000):
if i in x: pass
e = time()
print(e-s, 'dict() time spent looking through the data')
# Using set()
x = set()
s = time()
for i in range(10000000):
num = round(random(), 6)
if not num in x:
x.add(num)
e = time()
print(e-s, len(x), 'Using set() for inserts')
s = time()
for i in range(10000000):
if i in x: pass
e = time()
print(e-s, 'set() time spent looking through the data')
# Using lists
x = []
s = time()
for i in range(10000000): # Half the ammount 5M instead of 10M
num = round(random(), 6)
if not num in x:
x.append(num)
e = time()
print(e-s, len(x), 'Using list()')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment