Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.