Code that ought to run fast, but can't due to Python limitations.

Stefan Behnel stefan_ml at behnel.de
Sun Jul 5 05:48:38 EDT 2009


Paul Rubin wrote:
> Steven D'Aprano writes:
>> Yes, I'm aware of that, but that's not what John's code is doing -- he's 
>> doing a series of if expr ... elif expr tests. I don't think a case 
>> statement can do much to optimize that.
> 
> The series of tests is written that way because there is no case
> statement available.  It is essentially switching on a bunch of
> character constants and then doing some additional tests in each
> branch.

Although doing some of the tests first and then checking the input
conditionally might be faster here.

Another idea: You could exchange the methods whenever self.contentModelFlag
changes, i.e. you'd have a "dataState_CDATA", a "dataState_PCDATA" etc.


> It could be that using ord(c) as an index into a list of functions
> might be faster than a dict lookup on c to get a function.

Rather unlikely, given that calling "ord(c)" involves a dict lookup for
"ord". You might get away with the pre-initialised keywords trick, though.


> I think
> John is hoping to avoid a function call and instead get an indexed
> jump within the Python bytecode for the big function.

Hmm, yes, the actual code inside the conditionals is pretty short, so the
call overhead might hurt here.

Stefan



More information about the Python-list mailing list