Refactoring; arbitrary expression in lists

Jeff Shannon jeff at
Thu Jan 13 13:22:28 EST 2005

Stephen Thorne wrote:

> As for the overall efficiency concerns, I feel that talking about any
> of this is premature optimisation. 

I disagree -- using REs is adding unnecessary complication and 
dependency.  Premature optimization is a matter of using a 
conceptually more-complicated method when a simpler one would do; REs 
are, in fairly simple cases such as this, clearly *not* simpler than 
dict lookups.

> The optimisation that is really
> required in this situation is the same as with any
> large-switch-statement idiom, be it C or Python. First one must do a
> frequency analysis of the inputs to the switch statement in order to
> discover the optimal order of tests!

But if you're using a dictionary lookup, then the frequency of inputs 
is irrelevant.  Regardless of the value of the input, you're doing a 
single hash-compute and (barring hash collisions) a single 
bucket-lookup.  Since dicts are unordered, the ordering of the literal 
(or of a set of statements adding to the dict) doesn't matter.

Jeff Shannon
Credit International

More information about the Python-list mailing list