Created
March 11, 2016 16:09
-
-
Save Torxed/b3872299c5659ea0ea09 to your computer and use it in GitHub Desktop.
Short timing example between dict, list and set in python.
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 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