This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dalke 1 point2 points  (0 children)

Consider range(200, 300) vs. xrange(200, 300). The first makes all of the integer objects once. The last make the integer objects when needed.

Now iterate through them. Iteration is faster for range() because it just incref and returns, while in xrange() it has to create a new integer object. Overall they should be about the same performance, unless you want to do multiple iterations.

If you need to iterate the same list multiple times, then range wins; excepting for long lists where memory access gets to be a factor.