Trinary operator?

Andrew Dalke dalke at dalkescientific.com
Fri Apr 19 16:11:32 EDT 2002


Neal Norwitz:

and/or
>0.248443961143

create the dictionary each time
>0.696393966675

create the dictionary once, and use get
>0.322777986526

create the dictionary once, and use try/except
>0.239256024361

One other thing skews these numbers.  You are looking up a module
variable each time for the last two timings, and that's a hash table
lookup.  If it's inside a function then it's a constant time lookup.
Here's the same timings on my machine, with the second batch of
timings done by code identical to the first, except inside of a
function.

All variables in module scope
0.382031083107
1.22617602348
0.645591974258
0.358752012253

All variables in function scope
0.266326904297
1.18248593807
0.544940948486
0.250100016594

You can see there are speedups all over the board, and the fastest
is still the try/except.

The reason the try/except is faster than the .get is because while
exceptions are slow, they are never called for your test (you always
use 'f').  The overhead for the [] on dicts is lower than the overhead
to create the bound method ".get".


                    Andrew
                    dalke at dalkescientific.com







More information about the Python-list mailing list