Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Sun, 20 Mar 2016 22:21:47 -0400 Lines: 38 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=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de K2vj2306bLXDQlgU23NT8wpBV+r0Ukv40wauxaaNy1bQ== 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; 'binary': 0.05; 'linear': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:which': 0.09; 'python': 0.10; 'python.': 0.11; 'jan': 0.11; '9:15': 0.16; 'clause.': 0.16; 'clauses': 0.16; 'hashed': 0.16; 'python;': 0.16; 're-written': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'statement)': 0.16; 'subject:?)': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'module,': 0.18; 'tests': 0.18; 'fairly': 0.22; 'trying': 0.22; 'code.': 0.23; 'represents': 0.23; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'define': 0.27; 'switch': 0.27; 'tired': 0.27; '---': 0.28; 'dictionary': 0.29; 'subset': 0.29; 'that.': 0.30; 'code': 0.30; 'task': 0.30; 'suit': 0.33; 'case,': 0.34; 'languages': 0.34; 'quickly': 0.34; 'functions.': 0.35; 'mapping': 0.35; 'replace': 0.35; 'replaced': 0.35; 'tasks': 0.35; 'express': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'faster': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; '(with': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'claim': 0.61; 'subject:The': 0.61; 'course': 0.62; 'received:96': 0.63; 'more': 0.63; 'believe': 0.66; 'idiomatic': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.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:105330 On 3/20/2016 9:15 PM, BartC wrote: > http://pastebin.com/dtM8WnFZ > This is a test of a character-at-a-time task in Python; I disagree. It tests of C code re-written in ludicrously crippled Python. No use of the re module, designed for tasks like this, and no use of dicts, which replace many uses of switch statements. > but exactly such tasks are what I often use dynamic languages for. For instance, there are about 15 clauses like --- elif c=="?": lxsymbol=question_sym return --- I believe it would be much faster to combine these in one clause. First define simple_symbols = {'?': question_sym, ...}. Then elif c in simple_symbols: lxsymbol = simple_symbols[c] return In any case, the O(k), where k is the number of alternatives, linear search should be replaced by an O(log k) binary search (nested if-else statement) or O(1) hashed search (with a dictionary mapping chars to functions. > I started off trying to write it in a more efficient way that would suit > Python better, but quickly tired of that. I should be able to express > the code how I want. Of course you can. But you cannot write in a crippled Python subset and fairly claim that the result represents idiomatic Python code. -- Terry Jan Reedy