Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; ':-)': 0.06; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; '(line': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'undefined': 0.09; 'subject:" ': 0.10; '1.0),': 0.16; 'andreas': 0.16; 'continue;': 0.16; 'overflow': 0.16; 'reedy': 0.16; 'subject:=': 0.16; 'wrote:': 0.16; 'int': 0.18; 'jan': 0.19; 'memory': 0.21; 'maybe': 0.21; 'header:In-Reply-To:1': 0.22; 'precise': 0.23; 'pm,': 0.24; 'code': 0.25; 'code.': 0.26; 'bit': 0.28; 'skip:( 20': 0.29; 'clear,': 0.30; 'construct': 0.30; 'goto': 0.30; 'null)': 0.30; 'source': 0.33; 'there': 0.33; 'to:addr:python- list': 0.33; 'difference': 0.34; 'however,': 0.34; 'someone': 0.34; 'header:User-Agent:1': 0.34; 'header:X-Complaints-To:1': 0.35; 'uses': 0.35; 'using': 0.37; 'received:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'else': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'subject: (': 0.39; "it's": 0.40; 'huge': 0.61; 'subject:+': 0.73; '1346': 0.84; '1543': 0.84; '1548': 0.84; '1550': 0.84; '1559': 0.84; '1560': 0.84; 'consumed': 0.84; 'complexity': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Date: Sun, 21 Aug 2011 15:39:53 -0400 References: <4e513ceb$0$23863$e4fe514c@news2.news.xs4all.nl> <1313947658.3424.3.camel@thegeorge> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: <1313947658.3424.3.camel@thegeorge> 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313955635 news.xs4all.nl 23847 [2001:888:2000:d::a6]:39433 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11957 On 8/21/2011 1:27 PM, Andreas L=C3=B6scher wrote: >> What the precise difference (semantics and speed) is between the >> BINARY_ADD and INPLACE_ADD opcodes, I dunno. Look in the Python source= >> code or maybe someone knows it from memory :-) >> >> Irmen >> > from Python/ceval.c: > > 1316 case BINARY_ADD: > 1317 w =3D POP(); > 1318 v =3D TOP(); > 1319 if (PyInt_CheckExact(v)&& PyInt_CheckExact(w)) { > 1320 /* INLINE: int + int */ > 1321 register long a, b, i; > 1322 a =3D PyInt_AS_LONG(v); > 1323 b =3D PyInt_AS_LONG(w); > 1324 /* cast to avoid undefined behaviour > 1325 on overflow */ > 1326 i =3D (long)((unsigned long)a + b); > 1327 if ((i^a)< 0&& (i^b)< 0) > 1328 goto slow_add; > 1329 x =3D PyInt_FromLong(i); > 1330 } > 1331 else if (PyString_CheckExact(v)&& > 1332 PyString_CheckExact(w)) { > 1333 x =3D string_concatenate(v, w, f, next_instr); > 1334 /* string_concatenate consumed the ref to v */ > 1335 goto skip_decref_vx; > 1336 } > 1337 else { > 1338 slow_add: > 1339 x =3D PyNumber_Add(v, w); > 1340 } > 1341 Py_DECREF(v); > 1342 skip_decref_vx: > 1343 Py_DECREF(w); > 1344 SET_TOP(x); > 1345 if (x !=3D NULL) continue; > 1346 break; > > 1532 case INPLACE_ADD: > 1533 w =3D POP(); > 1534 v =3D TOP(); > 1535 if (PyInt_CheckExact(v)&& PyInt_CheckExact(w)) { > 1536 /* INLINE: int + int */ > 1537 register long a, b, i; > 1538 a =3D PyInt_AS_LONG(v); > 1539 b =3D PyInt_AS_LONG(w); > 1540 i =3D a + b; > 1541 if ((i^a)< 0&& (i^b)< 0) > 1542 goto slow_iadd; > 1543 x =3D PyInt_FromLong(i); > 1544 } > 1545 else if (PyString_CheckExact(v)&& > 1546 PyString_CheckExact(w)) { > 1547 x =3D string_concatenate(v, w, f, next_instr); > 1548 /* string_concatenate consumed the ref to v */ > 1549 goto skip_decref_v; > 1550 } > 1551 else { > 1552 slow_iadd: > 1553 x =3D PyNumber_InPlaceAdd(v, w); > 1554 } > 1555 Py_DECREF(v); > 1556 skip_decref_v: > 1557 Py_DECREF(w); > 1558 SET_TOP(x); > 1559 if (x !=3D NULL) continue; > 1560 break; > > As for using Integers, the first case (line 1319 and 1535) are true and= > there is no difference in Code. However, Python uses a huge switch-case= > construct to execute it's opcodes and INPLACE_ADD cames after > BINARY_ADD, hence the difference in speed. > > To be clear, this is nothing you should consider when writing fast code= =2E > Complexity wise they both are the same. With 64 bit 3.2.2 on my Win 7 Pentium, the difference was 4% and with=20 floats (0.0 and 1.0), 6% --=20 Terry Jan Reedy