Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'chunk': 0.07; 'francis': 0.07; 'subject:while': 0.07; 'difference,': 0.09; 'erik': 0.09; 'iterate': 0.09; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; 'cc:addr:python-list': 0.15; 'this:': 0.15; '"while': 0.16; 'latter,': 0.16; 'socket.': 0.16; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; '\xa0if': 0.23; 'cc:2**0': 0.25; 'import': 0.27; 'url:code': 0.28; 'message- id:@mail.gmail.com': 0.28; 'explicitly': 0.28; 'cc:addr:python.org': 0.29; 'times.': 0.30; '---': 0.31; 'break': 0.32; "won't": 0.33; 'instead': 0.33; 'too': 0.34; 'rather': 0.34; 'certain': 0.34; 'from:charset:iso-8859-1': 0.34; '...': 0.35; 'something': 0.35; 'file': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'received:209.85': 0.38; 'think': 0.38; 'being': 0.39; 'received:209': 0.39; 'subject:: ': 0.39; 'most': 0.60; 'url:p': 0.62; 'skip:1 10': 0.63; 'defensive': 0.84; 'imagine,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=aRpeleshiuBjCF6pXMMWObRAlSpJgeNEs83keRuTv5g=; b=pA4rVNTiS7ysYxJ9yHoUWQufoNQMWBrYTtopiHf65YjCn7KpWiFmdTETZETs1sk4m4 p/94ZzTYf8V22a6fdEwx3dztyqbd35mNzLGlVh5ZomGOijXGieO89/w/LowAIjhtaycg +Z6XETztI50+8Boeq6g1dfYRhJfNckXy+Q5os= MIME-Version: 1.0 In-Reply-To: References: <4F1AC1D4.2080402@gmail.com> Date: Mon, 23 Jan 2012 17:41:52 +0100 Subject: Re: while True or while 1 From: =?ISO-8859-1?Q?Giampaolo_Rodol=E0?= To: Erik Max Francis 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327336916 news.xs4all.nl 6887 [2001:888:2000:d::a6]:40352 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19269 Il 21 gennaio 2012 22:13, Erik Max Francis 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. = =A0If > it's the latter, it's a case of being _way_ too defensive. It's also because while 1 is faster: import time t =3D time.time() x =3D 0 while 1: x +=3D 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 =3D 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/