while True or while 1

Steve Holden steve at holdenweb.com
Thu Dec 16 07:23:40 EST 2010


On 12/16/2010 5:44 AM, BartC wrote:
>> On 12/12/2010 2:32 PM, Christian Heimes wrote:
>>> Am 12.12.2010 19:31, schrieb Steve Holden:
>>> $ python -m timeit -n20 -- "i = 0" "while 1:" "    i+=1" "    if i ==
>>> 1000000: break"
>>> 20 loops, best of 3: 89.7 msec per loop
>>> $ python -m timeit -n20 -- "i = 0" "while True:" "    i+=1" "    if i ==
>>> 1000000: break"
>>> 20 loops, best of 3: 117 msec per loop
> 
>>> No argue with that! I was merely making a point that "while 1" executes
>>> different byte code than "while True". Readability is important but
>>> sometimes speed is of the essence. "while 1" is one of the few tricks to
>>> speed up tight loops a bit.
>>
>> OK, but the figures you quote save you 27.3 ms per million iterations,
>> for a grand total saving of 27.3 ns per iteration. So "a bit" is hardly
>> worth considering for most programs, is it?
> 
> One these is 30% faster than the other. That's an appreciable
> difference, which you can't really just dismiss.
> 
> And you can't tell what the overall effect on a program will be: perhaps
> the loop will be in a library function , which might be called billions
> of times.

It might. But if the code it is calling does *anything* at all
significant I can promise you that spending an extra 30% purely on the
looping construct will still make a negligible difference to a program's
execution time, and there are almost certainly going to be many other
aspects of performance that will yield greater benefits from tuning.

I realise that there are going to be some people who just flatly say
"since 'while 1:' is quicker I am going to use it every time", and that
their programs will still work. And I still maintain that (for English
speakers) "while True:" is to be preferred.

sholden at lifeboy ~
$ python -m timeit -- "i = 1" "while True:" "    i += 1" "    if i ==
1000000: break"
10 loops, best of 3: 157 msec per loop

sholden at lifeboy ~
$ python -m timeit -- "i = 1" "while True:" "    i += 1" "    if i ==
1000000: break" "    x = i+1"
10 loops, best of 3: 238 msec per loop

sholden at lifeboy ~
$ python -m timeit -- "i = 1" "while 1:" "    i += 1" "    if i ==
1000000: break"
10 loops, best of 3: 116 msec per loop

sholden at lifeboy ~
$ python -m timeit -- "i = 1" "while 1:" "    i += 1" "    if i ==
1000000: break" "    x = i+1"
10 loops, best of 3: 195 msec per loop

If binding a simple arithmetic expression adds more to the loop than the
difference between "while 1:" and "while True:" you are wasting your
time thinking about the savings under all but the most rigorous
circumstances.

Fortunately there is no penalty for ignoring my advice.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17       http://us.pycon.org/
See Python Video!       http://python.mirocommunity.org/
Holden Web LLC                 http://www.holdenweb.com/




More information about the Python-list mailing list