efficient intersection of lists with rounding

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Fri Dec 3 05:11:48 EST 2004


Steven Bethard wrote:

> Yeah, almost certainly since he's looking at lists 3K long.  If they 
> were small, you never know since the list comprehension gets the C-code 
> speedup, while sets.Set is Python code:
>
 > [list comprehension]
> 10000 loops, best of 3: 27.5 usec per loop
> 
 > [Python 2.3 Set]
> 10000 loops, best of 3: 47.7 usec per loop
> 
> In the case given, the O(n**2) list comprehension is faster than the 
> O(n) set intersection.  Of course, this is not likely to be true with 
> any reasonable sized data.  But it's something worth keeping in mind.

Of course if you're working with a dataset that small, it probably 
doesn't really matter which of these implementations you use.

The exception would be if this were in an inner loop in the actual 
program and *were* being run 10000 times or more.
-- 
Michael Hoffman



More information about the Python-list mailing list