Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11984
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.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 | <me+list/python@ixokai.io> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'python.': 0.04; 'context': 0.04; '(especially': 0.07; 'behave': 0.07; 'behavior.': 0.07; 'chunk': 0.07; 'interpreter': 0.07; 'also:': 0.09; 'content-type:multipart/signed': 0.09; 'filename:fname piece:signature': 0.09; 'immutable': 0.09; 'kinda': 0.09; 'mutable': 0.09; 'subject:" ': 0.10; '"n"': 0.16; "'n'": 0.16; '(at)': 0.16; '(dot)': 0.16; '(its': 0.16; '1",': 0.16; '1.0),': 0.16; 'content-type:application/pgp-signature': 0.16; 'doing:': 0.16; 'encouraged,': 0.16; 'example):': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname:signature.asc': 0.16; 'intimate': 0.16; 'is).': 0.16; 'meaningful,': 0.16; 'predict': 0.16; 'predictable': 0.16; 'received:209.85.213.174': 0.16; 'received:mail-yx0-f174.google.com': 0.16; 'safe,': 0.16; 'subject:=': 0.16; 'syntactical': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'say,': 0.19; "aren't": 0.21; 'blog:': 0.21; "doesn't": 0.22; 'assume': 0.22; 'header:In-Reply-To:1': 0.22; 'fundamental': 0.23; 'objects,': 0.23; 'picking': 0.23; 'similar,': 0.23; 'pm,': 0.24; 'cache': 0.24; "python's": 0.24; 'fact': 0.27; 'all,': 0.28; 'depends': 0.28; 'described': 0.28; 'loop': 0.28; 'modify': 0.28; 'bit': 0.28; 'affected': 0.29; 'fine.': 0.29; "won't": 0.29; 'actions.': 0.30; 'augmented': 0.30; 'binding': 0.30; 'hansen': 0.30; 'lot.': 0.30; 'equivalent': 0.31; 'received:24': 0.32; 'assigned': 0.32; 'generally': 0.32; 'objects': 0.32; 'usually': 0.32; "what's": 0.33; 'probably': 0.33; "can't": 0.33; 'too': 0.33; 'to:addr:python-list': 0.33; 'that,': 0.33; "i've": 0.34; 'difference': 0.34; '...': 0.34; 'be.': 0.34; 'things': 0.34; 'all.': 0.34; 'assignment': 0.34; 'explicit': 0.34; 'like:': 0.34; 'here,': 0.35; 'pretty': 0.35; 'object': 0.35; 'certain': 0.35; 'anything': 0.36; 'doing': 0.36; 'using': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'could': 0.38; 'think': 0.38; 'returning': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'else': 0.39; 'basic': 0.39; 'goes': 0.39; 'extend': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'or,': 0.40; "it's": 0.40; 'more': 0.60; 'easily': 0.61; 'your': 0.61; 'kind': 0.61; 'details': 0.65; 'said:': 0.67; 'attention': 0.69; 'details,': 0.70; 'care': 0.71; 'header:Reply-To:1': 0.71; 'reply- to:no real name:2**0': 0.71; 'mail:': 0.73; 'subject:+': 0.73; '12:53': 0.84; 'akin': 0.84; 'anyways.': 0.84; 'dict,': 0.84; 'machine,': 0.84; 'self.': 0.84; 'supposedly': 0.84; 'seriously,': 0.91; 'slip': 0.91; 'url:io': 0.93; 'beneficial': 0.95 |
| Sender | Ixokai <ixokai@ixokai.io> |
| Date | Sun, 21 Aug 2011 19:08:22 -0700 |
| From | Stephen Hansen <me+list/python@ixokai.io> |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") |
| References | <4e513ceb$0$23863$e4fe514c@news2.news.xs4all.nl> <1313947658.3424.3.camel@thegeorge> <mailman.285.1313955635.27778.python-list@python.org> <747a8223-0a9b-4691-8886-0a04433e54dc@glegroupsg2000goo.googlegroups.com> |
| In-Reply-To | <747a8223-0a9b-4691-8886-0a04433e54dc@glegroupsg2000goo.googlegroups.com> |
| X-Enigmail-Version | 1.1.1 |
| OpenPGP | id=555B1FE5; url=http://id.ixokai.io/pub.asc |
| Content-Type | multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig3463576FDC68770A6E103663" |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| Reply-To | me+list/python@ixokai.io |
| 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.296.1313978913.27778.python-list@python.org> (permalink) |
| Lines | 116 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1313978913 news.xs4all.nl 23936 [2001:888:2000:d::a6]:60883 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:11984 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On 8/21/11 12:53 PM, Laurent wrote:
>
>> With 64 bit 3.2.2 on my Win 7 Pentium, the difference was 4% and with
>> floats (0.0 and 1.0), 6%
>
> For floats it is understandable. But for integers, seriously, 4% is a lot. I would never have thought an interpreter would have differences like this in syntax for something as fundamental as adding 1.
Its, seriously, not even kind of a lot at all. Percentages without
context are meaningless: its 4% slower, sure -- but that is 4% of an
incredibly small probably constant-time amount of time.
Picking "i += 1" over "i = i + 1" based on one being 4% slower is sorta
kinda crazy. The difference in speed is probably related to churn and
cache as much as anything else (its not as consistent on my machine, for
example): or the ceval loop doing a few more case-tests between them as
others have mentioned. All in all, if 4% of a nanomicrofraction of a
chunk of time is that meaningful, you're probably best served not using
Python. :)
That said: my advice is always to avoid += like a plague. It is magic
and impossible to predict without intimate knowledge of exactly what's
on the left-side.
i += 1
n += x
Those two things look very similar, but they may do -completely-
different things depending on just what "n" is.
It may or may not do something that is like:
n = n + x
Or, it may do something that's more akin to
n.extend(x)
n = n
Those aren't even kind of equivalent actions. And things get more
complicated if 'n' is say, n[0] (especially if something goes wrong
between the extend and the rebinding).
Python's usually all explicit and pretty well-defined in how its basic
syntax and behaviors operate, and you usually don't really have to know
details about how a data-type works to predict exactly what it's doing:
in fact, its often beneficial to not pay too much attention to such
details, and just assume the data type will work approximately as you'd
expect. That way people can slip something-something to you and wink and
say of /course/ its a dict, darling. Try it, you'll like it, okay? This
sorta thing is encouraged, but it kinda depends on trusting objects to
behave a certain way and for things to be predictable in both how they
work and how they fail.
With "i = i + 1", I know that generally speaking, my "i" is being
assigned a new object and that's that, no matter what type "i" is.
(Okay: I do know that you could modify __add__ to do something
underhanded here, tweaking internal state and then returning self.
People going out of their way to behave unpredictably is not my
objection: supposedly easy and straight-forward normal Python-fu being
inherently unpredictable is).
For example: I just /know/ that it doesn't matter who or what may have
their own binding to that object before I go and increment it, they
won't be affected and everything just will work fine. With augmented
assignment, I can't be sure of that. Now, while I admit, you generally
do have to keep track in your head of which of your data-types are
mutable vs immutable and take care with sharing mutables, the fact that
"n += x" is described and generally thought of as merely syntactical
sugar for:
n = n + x
... lets one easily think that this should be entirely safe, even with
mutable objects, because if += were merely syntactical sugar, it would
be. But its not! Because += is wiggly. It can do more then one entirely
different kind of behavior.
Anyways. </rant> I've been kinda annoyed at augmented assignment for
years now :P
--
Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-21 19:27 +0200
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 10:48 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 11:03 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Christian Heimes <lists@cheimes.de> - 2011-08-21 20:24 +0200
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Roy Smith <roy@panix.com> - 2011-08-21 14:52 -0400
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-22 01:17 +0200
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Chris Angelico <rosuav@gmail.com> - 2011-08-22 00:37 +0100
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Terry Reedy <tjreedy@udel.edu> - 2011-08-21 19:38 -0400
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-22 02:00 +0200
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Seebs <usenet-nospam@seebs.net> - 2011-08-22 05:33 +0000
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Terry Reedy <tjreedy@udel.edu> - 2011-08-21 15:39 -0400
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:53 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:55 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:55 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 11:12 +1000
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") "Richard D. Moores" <rdmoores@gmail.com> - 2011-08-22 02:55 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Emile van Sebille <emile@fenx.com> - 2011-08-22 09:35 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Emile van Sebille <emile@fenx.com> - 2011-08-22 10:22 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 13:04 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Laurent <laurent.payot@gmail.com> - 2011-08-21 12:53 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Andreas Löscher <andreas.loescher@s2005.tu-chemnitz.de> - 2011-08-22 01:25 +0200
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Chris Angelico <rosuav@gmail.com> - 2011-08-22 00:41 +0100
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 11:16 +1000
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Christian Heimes <lists@cheimes.de> - 2011-08-22 04:04 +0200
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Terry Reedy <tjreedy@udel.edu> - 2011-08-21 22:11 -0400
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Stephen Hansen <me+list/python@ixokai.io> - 2011-08-21 19:08 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-22 14:14 +1000
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Stephen Hansen <me+list/python@ixokai.io> - 2011-08-21 21:37 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Stephen Hansen <me+list/python@ixokai.io> - 2011-08-21 21:49 -0700
Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") casevh <casevh@gmail.com> - 2011-08-21 21:14 -0700
csiph-web