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: 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 Date: Sun, 21 Aug 2011 19:08:22 -0700 From: Stephen Hansen 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> <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig3463576FDC68770A6E103663 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 8/21/11 12:53 PM, Laurent wrote: >=20 >> With 64 bit 3.2.2 on my Win 7 Pentium, the difference was 4% and with = >> floats (0.0 and 1.0), 6% >=20 > For floats it is understandable. But for integers, seriously, 4% is a l= ot. 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 +=3D 1" over "i =3D i + 1" based on one being 4% slower is sor= ta 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 +=3D like a plague. It is magic and impossible to predict without intimate knowledge of exactly what's on the left-side. i +=3D 1 n +=3D 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 =3D n + x Or, it may do something that's more akin to n.extend(x) n =3D 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 =3D 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 +=3D x" is described and generally thought of as merely syntactical sugar for: n =3D n + x =2E.. lets one easily think that this should be entirely safe, even with mutable objects, because if +=3D were merely syntactical sugar, it would be. But its not! Because +=3D is wiggly. It can do more then one entirely= different kind of behavior. Anyways. I've been kinda annoyed at augmented assignment for years now :P --=20 Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/ --------------enig3463576FDC68770A6E103663 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.10 (Darwin) iQEcBAEBAgAGBQJOUboaAAoJEKcbwptVWx/l0bUH/0XTvNSVtbZopA0ZKdRIUqZv iOmCyX22PK7MSc90NrTp/uoLUsZEyUt/hIEWmj3EIyDFyFb3VbNyHkXNzqDc3DXy wKzhbt7/wcL3Zoxx8uai1BX6+wHgfrR+ycVf43hhtorCL0LEoERLA70Tgkmsl7a0 oxAGqhTl7WRcLJ7CA9Ayza7VIloWVbrWZGajHr/8ZMcNXtdsD4td0XwAOTIe5Q0k H8aPEYTlaqaSe8sZgcBNvMWqUiR3J2c2tm2DUGrjyBW/51Q68UT5tj9kno+lyVGg CqKq3HCnLOAkaIMDCFSu7x9xyOsPqqjSH8dcLnfiKYpUtfMBYkgJgDksAM7OPd0= =zsLA -----END PGP SIGNATURE----- --------------enig3463576FDC68770A6E103663--