I realize this is a older post. But, I arrived here looking for performance speeds comparing using intersection
vs in
and thought it'd be worth adding more info. The answers above were great, but left me unclear as to the actual best path forward.
The "first result" solution doesn't solve for my use case specifically.
Instead, I wanted to know how the different implementations would perform, producing identical results sets, using discrete approaches. Not just the first single intersected value. As such, below I've included code to perform an evaluation of the options with a 1000 loop test. Contrary to what @agf posted, using sets is far faster when the desired output is a list of matches.
My results were:
runForin took 132851.600ms
runForinBlist took 37700.916ms
True
runForInListComp took 132783.147ms
True
runForinSet took 780.919ms
True
runSetIntersection took 760.980ms (WINNER)
True
runSetin took 771.921ms
True
Here's the code. Hope it helps someone. Note: I also evaluated the blist (http://stutzbachenterprises.com/blist/blist.html) library as it performs quite well in other use cases.
import time
from random import sample, shuffle
from blist import blist
a = range(100000)
aBlist = blist([i for i in a])
b = sample(a, 1000)
a.reverse()
def print_timing(func):
def wrapper(*arg):
t1 = time.time()
res = func(*arg)
t2 = time.time()
print '%s took %0.3fms' % (func.func_name, (t2-t1)*1000.0)
return res
return wrapper
def forIn():
ret = []
for obj in b:
if obj in a:
ret.append(obj)
return ret
def forInBlist():
ret = []
for obj in b:
if obj in aBlist:
ret.append(obj)
return ret
def forInListComp():
return [value for value in b if value in a]
def forInSet():
ret = []
for obj in b:
if obj in set(a):
ret.append(obj)
return ret
def setIntersection():
return set(a).intersection(b)
def setIn():
return list(set(a) & set(b))
@print_timing
def runForIn(times):
for i in range(times):
ret = forIn()
return ret
@print_timing
def runForInBlist(times):
for i in range(times):
ret = forInBlist()
return ret
@print_timing
def runForInListComp(times):
for i in range(times):
ret = forInListComp()
return ret
@print_timing
def runForInSet(times):
for i in range(times):
ret = forInSet()
return ret
@print_timing
def runSetIntersection(times):
for i in range(times):
ret = setIntersection()
return ret
@print_timing
def runSetIn(times):
for i in range(times):
ret = setIn()
return ret
def checkResults(results):
master = None
for resultSet in results:
if not master:
master = sorted(list(resultSet))
continue
try:
if master != sorted(list(resultSet)):
return False, master, sorted(list(resultSet))
except:
print resultSet
return False
return True
iterations = 5
results = []
runForInResults = runForIn(iterations)
results.append(runForInResults)
runForInBlistResults = runForInBlist(iterations)
results.append(runForInBlistResults)
print checkResults(results)
runForInListCompResults = runForInListComp(iterations)
results.append(runForInListCompResults)
print checkResults(results)
runForInSetResults = runForInSet(iterations)
results.append(runForInSetResults)
print checkResults(results)
runSetIntersectionResults = runSetIntersection(iterations)
results.append(runSetIntersectionResults)
print checkResults(results)
runSetInResults = runSetIn(iterations)
results.append(runSetInResults)
print checkResults(results)
intersection
will keep going until it's found them all, but on the other hand, intersection is implemented in C, so the actual code will run faster.filter(other_set.__contains__, some_set)
and the differencefilter(lambda x: other_set.__contains__(x), some_set)
.