Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'thread,': 0.04; 'compiler': 0.07; 'francis': 0.07; 'subject:while': 0.07; 'python': 0.08; 'erik': 0.09; 'anyway': 0.09; 'am,': 0.12; 'cc:addr:python-list': 0.15; 'mon,': 0.15; 'this:': 0.15; '"while': 0.16; '(say': 0.16; 'chunks': 0.16; 'latter,': 0.16; 'lookup': 0.16; 'made,': 0.16; 'socket.': 0.16; 'wrote:': 0.16; '2.x': 0.18; 'jan': 0.19; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'pointed': 0.21; 'header:In-Reply-To:1': 0.22; 'literal': 0.23; 'moreover,': 0.23; '\xa0if': 0.23; 'received:74.125.82.174': 0.24; 'cc:2**0': 0.25; "wasn't": 0.25; 'seconds': 0.25; 'all,': 0.27; 'code,': 0.27; 'compared': 0.28; 'message- id:@mail.gmail.com': 0.28; 'anyway.': 0.28; 'explicitly': 0.28; 'cc:addr:python.org': 0.29; 'cycles': 0.30; 'sleep': 0.30; 'usually': 0.30; 'time:': 0.32; 'actually': 0.32; 'instead': 0.33; 'there': 0.33; 'loop': 0.34; 'too': 0.34; 'rather': 0.34; 'file.': 0.34; 'anything': 0.34; '"we': 0.34; 'root': 0.34; 'received:74.125.82': 0.34; '...': 0.35; 'something': 0.35; 'test': 0.35; 'file': 0.35; 'optimization': 0.36; 'received:74.125': 0.37; 'unless': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'useful': 0.38; 'think': 0.38; 'should': 0.38; 'processing': 0.39; 'point': 0.39; 'being': 0.39; 'doing': 0.39; 'subject:: ': 0.39; 'might': 0.40; 'difference': 0.40; 'more': 0.61; 'worth': 0.61; 'donald': 0.67; '100': 0.69; 'transfer': 0.72; 'million': 0.76; 'to:charset:iso-8859-1': 0.82; '1.\xa0': 0.84; '97%': 0.84; 'defensive': 0.84; 'difference.': 0.84; 'evil"': 0.84; 'imagine,': 0.84; 'knuth': 0.84; 'premature': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=cOdNI1UN56bkp61Qo6P3Xz4bQ9+QRRLIPRKpLgnXcwM=; b=eQCPs0EXo5m8vcN2Ug8sgQ//IgFmu3sJIcGe+gUoUKb1h3gO1BOYJIvEppvIW52oyo ueaoaH5tdLhz1cH+VhJgHNeobNruIS8E2Fffwy1yp9RqCw8tihB0HYKcbYEiEAy1ssVg rJVEa55W9IfJQYhw6ROz80WfIw/iAc8tHTBnA= MIME-Version: 1.0 In-Reply-To: References: <4F1AC1D4.2080402@gmail.com> From: Ian Kelly Date: Mon, 23 Jan 2012 10:39:41 -0700 Subject: Re: while True or while 1 To: =?ISO-8859-1?Q?Giampaolo_Rodol=E0?= Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327340413 news.xs4all.nl 6936 [2001:888:2000:d::a6]:43105 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19270 On Mon, Jan 23, 2012 at 9:41 AM, Giampaolo Rodol=E0 wr= ote: > > Il 21 gennaio 2012 22:13, Erik Max Francis ha scritto: > > The real reason people still use the `while 1` construct, I would imagi= ne, > > is just inertia or habit, rather than a conscious, defensive decision. = =A0If > > it's the latter, it's a case of being _way_ too defensive. > > It's also because while 1 is faster: That's because, as has already been pointed out in the thread, the compiler is able to store the 1 literal with the code, whereas "True" requires a name lookup.=A0 If you try the same timing test in Python 3, you will find that there is no longer any difference. > Think about receiving a 10 GB file by using a socket. You'd tipically > have something like this: > > while 1: > =A0 =A0chunk =3D sock.recv(1024): > =A0 =A0if not chunk: > =A0 =A0 =A0 =A0 =A0break > =A0 =A0 ... > > > Now, that's a case where I (personally) want to explicitly use "while > 1" instead of "while True". I disagree.=A0 10 GB in 1 KB chunks is going to be 10,000,000 loops, which as you just demonstrated above, carries an overhead of about 0.4 seconds if we use True instead of 1.=A0 That's entirely trivial compared to the time needed to actually transfer the file. Moreover, unless you're doing a significant amount of processing on the file as you read it, a socket-reading loop like that is likely to be IO-bound -- which means that after the "while 1" check, the process is usually going to go to sleep anyway as it waits for more data.=A0 In that case, the extra 40 nanoseconds for the name lookup makes no difference at all, as the process wasn't going to do anything useful with those cycles anyway. Actually, I have a hard time envisioning a case where the difference between "while 1" and "while True" in Python 2.x is worth caring about.=A0 At the point where it might actually start to be noticeable (say around 100 million iterations), there are surely higher-priority optimizations to be made, including rewriting parts of the program in C. "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" -- Donald Knuth Cheers, Ian