Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Tue, 22 Mar 2016 00:42:22 +0000 Lines: 37 Message-ID: References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de tso1wnKzgkZRJUvmtmvtyQZLKsTGFdqSKKiYGj1bNnXA== 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; 'handler': 0.04; 'from:addr:yahoo.co.uk': 0.05; 'dict': 0.09; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:which': 0.09; 'stored': 0.10; 'python': 0.10; 'python.': 0.11; '(but': 0.15; 'attaching': 0.16; 'received:194.126': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'recipes': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'statements,': 0.16; 'subject:?)': 0.16; 'umpteen': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'tests': 0.18; 'language': 0.19; '>>>': 0.20; 'either.': 0.22; 'lawrence': 0.22; 'am,': 0.23; 'help.': 0.23; 'attach': 0.23; "python's": 0.23; 'implemented': 0.24; 'header:In- Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'handling': 0.27; 'switch': 0.27; 'function': 0.28; 'code': 0.30; 'entry': 0.31; 'implement': 0.32; 'language.': 0.32; 'reported': 0.32; 'problem': 0.33; 'doubt': 0.33; 'surprised': 0.33; 'list': 0.34; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'faster': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'wrong': 0.38; 'why': 0.39; "didn't": 0.39; 'expressed': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'some': 0.40; 'subject:The': 0.61; 'received:194': 0.61; 'charset:windows-1252': 0.62; 'our': 0.64; 'stated': 0.70; 'dict.': 0.84; 'dramatically': 0.84; 'gut': 0.84; 'pythonistas,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 248.80.126.194.pool.dsl.daisyplc.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105426 On 22/03/2016 00:18, BartC wrote: > On 21/03/2016 23:50, Terry Reedy wrote: >> On 3/21/2016 8:43 AM, BartC wrote: >> >>> This tests highlights the benefits of an O(1) switch statement. >> >> So why are you not taking advantage of Python's O(1) dict lookup? >> > I've already reported using a list lookup which is also O(1), and it > didn't really help. I doubt a dict lookup is going to be faster (but > Python has surprised me before). Please explain how you managed to make the list lookup O(1). > > One problem is how to attach the handling code to the entry in the list > or as the value in the dict. With the list, I stored a function reference. It has repeatedly been stated that a dict is used as a switch replacement, so there is no problem attaching a handler to a dict. > > I suspect that a switch implemented Python-style wouldn't be > dramatically faster either. But the handling code can be expressed in-line. > There are umpteen recipes of all shapes and sizes that implement switch/case statements, even pattern matching, online. Why don't you time some of them, as relying on gut instinct is almost always wrong in Python. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence