Tue 26 Sep 2006
range vs. xrange in Python
Posted by dkaz under Python, Programming
I found myself questioning why others are looping using xrange, while I’ve been using the boring, non-x range.
Gotta love the fact that I was able to use the built-in in “help” module in Python to answer this quickly.
>>> help(xrange)
Help on class xrange in module __builtin__:class xrange(object)
| xrange([start,] stop[, step]) -> xrange object
|
| Like range(), but instead of returning a list, returns an object that
| generates the numbers in the range on demand. For looping, this is
| slightly faster than range() and more memory efficient.

March 16th, 2007 at 11:33 am
I tried a couple of tests with for loops using both range and xrange. some loops passed, others did stuff like math.sqrt(i) or random.choice(range(10)). Findings: range and xrange are equal with loops below 1000, xrange is slower around 1000-10000, but range is slower higher up, with mininal time.time() differences of like 0.05 seconds