range() is not the best way to check range?

Grant Edwards grante at visi.com
Tue Jul 18 00:13:05 EDT 2006


On 2006-07-18, Summercoolness at gmail.com <Summercoolness at gmail.com> wrote:

> it seems that range() can be really slow:
>
> the following program will run, and the last line shows how long it ran
> for:
>
> import time
>
> startTime = time.time()
>
> a = 1.0
> for i in range(0, 30000):
>     if i in range (0, 10000):
>         a += 1
>     if not i % 1000: print i
>
> print a, "   ", round(time.time() - startTime, 1), "seconds"


> or is there an alternative use of range() or something similar that can
> be as fast?

Creating and then searching a 10,000 element list to see if a
number is between two other numbers is insane.

Using xrange as somebody else suggested is also insane.

If you want to know if a number is between two other numders,
for pete's sake use the comparison operator like god intended.

    if 0 <= i <= 10000:

-- 
Grant Edwards                   grante             Yow!  ANN JILLIAN'S HAIR
                                  at               makes LONI ANDERSON'S
                               visi.com            HAIR look like RICARDO
                                                   MONTALBAN'S HAIR!



More information about the Python-list mailing list