Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!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; 'example:': 0.03; 'args': 0.05; 'pointer': 0.05; 'behave': 0.07; 'compiler': 0.07; '(unsigned': 0.09; 'default:': 0.09; 'enum': 0.09; 'fetch': 0.09; 'pointer.': 0.09; 'statement.': 0.09; 'subject:" ': 0.10; 'case.': 0.15; "(i'll": 0.16; 'andreas': 0.16; 'decrements': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'jmp': 0.16; 'offset,': 0.16; 'pointer,': 0.16; 'printf("no': 0.16; 'sequential': 0.16; 'subject:=': 0.16; 'looked': 0.16; 'otherwise,': 0.19; 'appropriate': 0.20; 'header:In-Reply-To:1': 0.22; 'compiled': 0.23; 'fine,': 0.23; 'code': 0.25; 'statement': 0.25; 'string': 0.26; 'all,': 0.28; 'constant': 0.29; 'message- id:@mail.gmail.com': 0.29; 'second': 0.29; 'turned': 0.30; 'shift': 0.30; 'table.': 0.30; 'values,': 0.30; 'translate': 0.31; 'chris': 0.32; 'values': 0.32; 'cases': 0.32; 'actually': 0.33; 'to:addr:python-list': 0.33; 'done': 0.34; 'quite': 0.34; 'integer': 0.34; 'uses': 0.35; 'switch': 0.35; 'optimization': 0.36; 'using': 0.37; 'several': 0.37; 'open': 0.37; 'two': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'subject: (': 0.39; 'your': 0.61; 'full': 0.63; 'easily.': 0.67; 'subject:+': 0.73 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=2wA2QS2uGMU4L+KkzOoOwDrN1na+9LnMP5KR95fR3mI=; b=EGj25xp2XXwxJPqoZ6T59Ex8/xKS10tc6mavOSNyoq3KJYFdttkZPEMyiiML6kQ8/T C5Az94Ki51h8x3grg4GRxapj+vxaQwd6Rn3HSiG8gU6yj2WMwXGYAowpv3/T//n9e89Y +Wbioj6zqhaoInb33J5XD+l3x2jC7QBBd7uIk= MIME-Version: 1.0 In-Reply-To: <1313968634.3135.14.camel@thegeorge> References: <4e513ceb$0$23863$e4fe514c@news2.news.xs4all.nl> <1313947658.3424.3.camel@thegeorge> <1313968634.3135.14.camel@thegeorge> Date: Mon, 22 Aug 2011 00:37:45 +0100 Subject: Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313969874 news.xs4all.nl 23976 [2001:888:2000:d::a6]:51582 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11970 2011/8/22 Andreas L=F6scher : > How would such an jump table work to behave the same liek a > switch-case-statement? If your switch statement uses a simple integer enum with sequential values, then it can be done quite easily. Take this as an example: switch (argc) { case 0: printf("No args at all, this is weird\n"); break; case 1: printf("No args\n"); break; case 2: printf("Default for second arg\n"); case 3: printf("Two args\n"); break; default: printf("Too many args\n"); break; } I compiled this using Open Watcom C, looked at the disassembly, and hereby translate it into pseudocode (I'll email/post the full 80x86 disassembly if you like): 1) Check if argc > 3 (unsigned comparison), if so jump to default case. 2) Left shift argc two places, add a constant offset, fetch a pointer from there, and jump to it - that's the jump table. One JMP statement. 3) Code follows for each case. Incidentally, the Open Watcom compiler actually turned several of the cases into offset-load of the appropriate string pointer, and then a jump to the single call to printf. The fall-through from 'case 2' to 'case 3' works fine, although it means that 'case 2' has to be de-optimized from that one simplification. This type of optimization works best when the case values are sequential. (If I remove the 'case 0', the compiler decrements argc and proceeds to continue as above.) Otherwise, the jump table has to have a lot of copies of the "default" pointer. Chris Angelico