Skip to content

Instantly share code, notes, and snippets.

@mattions
Last active August 29, 2015 14:13
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 mattions/22e3fd090b0390451420 to your computer and use it in GitHub Desktop.
Save mattions/22e3fd090b0390451420 to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
data = []
for n_elements in [100, 1000, 10000, 100000]:
a = range(n_elements)
sa = set(a)
b = np.array(a) + 50
sb = set(b.tolist())
start = time.time()
c = []
for e in a:
if e in b:
c.append(e)
stop = time.time()
list_timing = stop - start
print "{0} Elements intersected in {1} seconds.".format(n_elements, list_timing)
print "Intersection result: {0}, last three elements: {1}.".format(len(c), c[-3:])
# Set intersection
start = time.time()
c2 = sa.intersection(sb)
stop = time.time()
set_timing = stop - start
print "Set Intersect - {0} Elements intersected in {1} seconds.".format(n_elements, set_timing)
print "Intersection result: {0}, last three elements: {1}.".format(len(c2), list(c2)[-3:])
print "-"*70
data.append({"elements" : n_elements, "list_timing" : list_timing, "set_timing" : set_timing})
#plotting it for convenience
df = pd.DataFrame(data)
df = df.set_index("elements")
plt.style.use("ggplot")
df.plot(style="o-", logx=True)
plt.ylabel("seconds")
plt.title("Quick benchmark test")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment