NEWBIE: Extending a For Statement.

Sion Arrowsmith siona at chiark.greenend.org.uk
Tue May 22 08:01:20 EDT 2007


 <bearophileHUGS at lycos.com> wrote:
>mosscliffe:
>> if key in xrange (60,69) or key == 3:
>I keep seeing again and again code like this, mostly from people not
>much expert of Python, but the PEP 260 shows the fast in was removed,
>so it's O(n).

If you're going to point that out, you should at least also mention
the "correct" formulation:

if 60 <= key < 69 or key == 3:

And if anyone cares about performance:
$ python -mtimeit -s 'x = 67' 'x in range(60, 69)'
1000000 loops, best of 3: 0.906 usec per loop
$ python -mtimeit -s 'x = 67' 'x in xrange(60, 69)'
1000000 loops, best of 3: 0.744 usec per loop
$ python -mtimeit -s 'x = 61' 'x in range(60, 69)'
1000000 loops, best of 3: 0.707 usec per loop
$ python -mtimeit -s 'x = 61' 'x in xrange(60, 69)'
1000000 loops, best of 3: 0.471 usec per loop
$ python -mtimeit -s 'x = 67' '60 <= x < 69'
10000000 loops, best of 3: 0.155 usec per loop

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
        -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list