speedup up dictionary access
Roy Smith
roy at panix.com
Sat May 12 17:21:32 EDT 2001
One of the big time consumers in an application I'm writing is a cache
lookup in the inner loop of the program. Right now, it looks something
like this:
if (self.cache.has_key (name) and self.cache[name]) or self.check (name):
do stuff
check() returns 0 or 1 based on the value of name, and also inserts this
value into the cache.
I know that my cache hit ratio is going to be very high (almost 100%).
Would I do better writing it as:
try:
if self.cache[name]:
do stuff
except KeyError:
if self.check (name):
do stuff
In the first style, I've got the overhead of doing a has_key() call which
99% of the time will return 1. In the second style, I've got the overhead
of setting up a try block. Which is faster?
More information about the Python-list
mailing list