Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19270
| 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 | <ian.g.kelly@gmail.com> |
| 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 | <CAFYqXL8PPOi2dy7M0ae+zCVB2yD6hPDdr7q8MhL0waadqoWvuw@mail.gmail.com> |
| References | <4F1AC1D4.2080402@gmail.com> <mailman.4909.1327157944.27778.python-list@python.org> <pL-dnUjq2eH3t4bSnZ2dnUVZ5vudnZ2d@giganews.com> <CAFYqXL8PPOi2dy7M0ae+zCVB2yD6hPDdr7q8MhL0waadqoWvuw@mail.gmail.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Mon, 23 Jan 2012 10:39:41 -0700 |
| Subject | Re: while True or while 1 |
| To | Giampaolo Rodolà <g.rodola@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4968.1327340413.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Mon, Jan 23, 2012 at 9:41 AM, Giampaolo Rodolà <g.rodola@gmail.com> wrote: > > Il 21 gennaio 2012 22:13, Erik Max Francis <max@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: 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. 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: > 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". I disagree. 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. 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. 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. 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: while True or while 1 Chris Angelico <rosuav@gmail.com> - 2012-01-22 01:59 +1100
Re: while True or while 1 Erik Max Francis <max@alcyone.com> - 2012-01-21 13:13 -0800
Re: while True or while 1 Chris Angelico <rosuav@gmail.com> - 2012-01-22 09:13 +1100
Re: while True or while 1 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-22 05:25 +0000
Re: while True or while 1 Dan Sommers <dan@tombstonezero.net> - 2012-01-22 16:05 +0000
Re: while True or while 1 alex23 <wuwei23@gmail.com> - 2012-01-22 19:55 -0800
Re: while True or while 1 Dave Angel <d@davea.name> - 2012-01-23 08:02 -0500
Re: while True or while 1 Hrvoje Niksic <hniksic@xemacs.org> - 2012-01-23 14:28 +0100
Re: while True or while 1 Dave Angel <d@davea.name> - 2012-01-23 13:33 -0500
Re: while True or while 1 MRAB <python@mrabarnett.plus.com> - 2012-01-22 16:13 +0000
Re: while True or while 1 Grant Edwards <invalid@invalid.invalid> - 2012-01-23 15:51 +0000
Re: while True or while 1 Giampaolo Rodolà <g.rodola@gmail.com> - 2012-01-23 17:41 +0100
Re: while True or while 1 Erik Max Francis <max@alcyone.com> - 2012-01-23 11:12 -0800
Re: while True or while 1 Giampaolo Rodolà <g.rodola@gmail.com> - 2012-01-23 21:20 +0100
Re: while True or while 1 Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-23 10:39 -0700
Re: while True or while 1 Evan Driscoll <edriscoll@wisc.edu> - 2012-01-23 12:05 -0600
Re: while True or while 1 Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-01-23 20:50 +0000
Re: while True or while 1 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-23 14:42 -0800
Re: while True or while 1 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-23 14:42 -0800
Re: while True or while 1 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-24 03:37 +0000
csiph-web