The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Terry Reedy tjreedy at udel.edu
Sun Mar 20 22:21:47 EDT 2016


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




More information about the Python-list mailing list