while True or while 1
Giampaolo Rodolà
g.rodola at gmail.com
Mon Jan 23 11:41:52 EST 2012
Il 21 gennaio 2012 22:13, Erik Max Francis <max at alcyone.com> ha scritto:
> The real reason people still use the `while 1` construct, I would imagine,
> is just inertia or habit, rather than a conscious, defensive decision. If
> it's the latter, it's a case of being _way_ too defensive.
It's also because while 1 is faster:
import time
t = time.time()
x = 0
while 1:
x += 1
if x > 10000000:
break
print(time.time() - t)
while True: 1.41121292114
while 1: 1.07101011276
Most of the times tha't won't make any noticeable difference, but it's
also true that certain while loops are going to iterate milions of
times.
Think about receiving a 10 GB file by using a socket. You'd tipically
have something like this:
while 1:
chunk = sock.recv(1024):
if not chunk:
break
...
Now, that's a case where I (personally) want to explicitly use "while
1" instead of "while True".
--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/
http://code.google.com/p/pysendfile/
More information about the Python-list
mailing list