On Feb 5, 2010, at 3:39 PM, Paul McNett wrote:
> You are in effect iterating through all of them in the list comp anyway. I
> break and
> return upon finding a match.
No, you aren't. While a list comp may be conceptually similar to the
corresponding for loop, it is greatly optimized internally. I remember being
challenged on this claim when I first started at Rackspace, so we set up code
to iterate through a loop, and the corresponding list comp. The results weren't
even close, and got worse for loops as the size got bigger.
Think of it like an SQL statement: list comps are like having an index
on the filtered column, while loops are like filtering on an unindexed column.
I don't know how it works under the hood in the C code, but I have done several
tests, and list comps beat loops by far. In your example, if the value you are
looking for is in one of the first records, it probably won't make a
difference, but if it is near the bottom or not present at all, it will be
much, much slower.
-- Ed Leafe
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message:
http://leafe.com/archives/byMID/[email protected]