Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'behave': 0.07; 'compiler': 0.07; 'linear': 0.07; 'reason.': 0.07; 'received:verizon.net': 0.07; 'supported.': 0.07; 'terry': 0.07; 'python': 0.08; '(line': 0.09; '>>>>': 0.09; 'executed': 0.09; 'integers': 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; 'sonntag,': 0.09; 'subject:" ': 0.10; 'algorithm': 0.13; 'entries': 0.13; 'void': 0.15; 'andreas': 0.16; 'chained': 0.16; 'dictionary).': 0.16; 'pointers': 0.16; 'reedy': 0.16; 'roy': 0.16; 'subject:=': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'bytes': 0.18; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'pm,': 0.24; 'byte': 0.24; 'code': 0.25; 'url:wiki': 0.25; 'statement': 0.25; 'code.': 0.26; 'array': 0.30; 'construct': 0.30; 'table.': 0.30; 'values': 0.32; 'this.': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'difference': 0.34; 'however,': 0.34; 'header:User-Agent:1': 0.34; 'things': 0.34; 'header:X-Complaints-To:1': 0.35; 'rather': 0.35; 'uses': 0.35; 'switch': 0.35; 'optimization': 0.36; 'using': 0.37; 'think': 0.38; 'received:org': 0.38; 'some': 0.38; 'url:org': 0.38; 'subject:: ': 0.39; 'url:en': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'subject: (': 0.39; "it's": 0.40; 'huge': 0.61; 'address': 0.61; 'fall': 0.64; 'bottom': 0.64; 'applicable': 0.68; 'relevant': 0.69; 'subject:+': 0.73; 'url:secure': 0.73; 'stream': 0.77; '14:52': 0.84; '1802': 0.84; 'article,': 0.84; 'compiles': 0.84; 'schrieb': 0.84; 'url:wikimedia': 0.84; 'hence,': 0.91; '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 19:38:22 -0400 References: <4e513ceb$0$23863$e4fe514c@news2.news.xs4all.nl> <1313947658.3424.3.camel@thegeorge> <1313968634.3135.14.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: <1313968634.3135.14.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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313969950 news.xs4all.nl 23876 [2001:888:2000:d::a6]:52380 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11971 On 8/21/2011 7:17 PM, Andreas L=C3=B6scher wrote: > Am Sonntag, den 21.08.2011, 14:52 -0400 schrieb Roy Smith: >> In article, >> Christian Heimes wrote: >> >>> Am 21.08.2011 19:27, schrieb Andreas Lscher: >>>> 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-c= ase >>>> construct to execute it's opcodes and INPLACE_ADD cames after >>>> BINARY_ADD, hence the difference in speed. >>> >>> I don't think that's the reason. Modern compiles turn a switch statem= ent >>> into a jump or branch table rather than a linear search like chained >>> elif statements. >> >> This is true even for very small values of "modern". I remember the >> Unix v6 C compiler (circa 1977) was able to do this. > > What is the difference in speed between a jump table that is searched > from top to bottom in comparison to an ordinary if-then-elif...? The > difference can only be in the search algorithm regarding the table. > Without optimization (linear search) both are the same. If the compiler= > applies some magic the difference can be relevant (linear complexity fo= r > if-then-elif... and O(1) if you would use a dictionary). A jump or branch table is applicable when the case value values are all=20 small ints, like bytes or less. For C, the table is simply an array of=20 pointers (addressess, with entries for unused byte codes would be a void = pointer). Hence, O(1) access. https://secure.wikimedia.org/wikipedia/en/wiki/Jump_table > Hence the executed code for integers is the same, there must be a slowe= r > path to the code of BINARY_ADD than to INPLACE_ADD. > > How would such an jump table work to behave the same liek a > switch-case-statement? Beware, that things like > > case PRINT_NEWLINE_TO: > 1802 w =3D stream =3D POP(); > 1803 /* fall through to PRINT_NEWLINE */ add jump to address of the code for PRINT_NEWLINE > 1804=09 > 1805 case PRINT_NEWLINE: > > must be supported. --=20 Terry Jan Reedy