[Python-Dev] exception too expensive?

tomer filiba tomerfiliba at gmail.com
Sat Jul 8 18:49:46 CEST 2006


i thought avoiding a second dict lookup should be faster, but it turned out
to be completely wrong. it's only marginally faster, but if an exception
occurs,
it's x10 slower.

#
# the code
#
>>> import time
>>> b = dict((i, 7) for i in range(1000))
>>> def try_lookup(k):
...     try:
...         return b[k]
...     except KeyError:
...         return 17
...
>>> def if_lookup(k):
...    if k in b:
...        return b[k]
...    return 17
...
>>> def test(f, k, count=100000):
...     t=time.time()
...     while count:
...         count -=1
...         f(k)
...     return time.time()-t
...

#
# try_lookup with valid key
#
>>> test(try_lookup, 56, 1000000)
0.6099998950958252
>>> test(try_lookup, 56, 1000000)
0.60899996757507324
>>> test(try_lookup, 56, 1000000)
0.6099998950958252

~0.61 sec

#
# if_lookup with valid key
#
>>> test(if_lookup, 56, 1000000)
0.68799996376037598
>>> test(if_lookup, 56, 1000000)
0.68700003623962402
>>> test(if_lookup, 56, 1000000)
0.67200016975402832

~0.68 sec

#
# try_lookup with invalid key
#
>>> test(try_lookup, 9456, 1000000)
7.062000036239624
>>> test(try_lookup, 9456, 1000000)
6.812000036239624
>>> test(try_lookup, 9456, 1000000)
6.8440001010894775

~6.90 sec

#
# if_lookup with invalid key
#
>>> test(if_lookup, 9456, 1000000)
0.625
>>> test(if_lookup, 9456, 1000000)
0.64100003242492676
>>> test(if_lookup, 9456, 1000000)
0.65599989891052246

~0.64 sec

======

before someone says "why don't you use dict.get", it's not applicable in
my code. i have a cache -- either object already exists in the cache, or i
create and store it in the cache -- so get() and setdefault() are not
useful.

so first of all, i'd recommend not using the try_lookup method; use the
if_lookup instead... unless you can be sure the changes a KeyError
will be raised are marginal.

second, isn't there anything that can be done to improve the performance
of exceptions? imo, exceptions should be cheap. i understand it has to
do with creating the exception instance everytime, but can't something
be done to improve that? for example, hold a cache exception instances
and just memcpy them when needed (just a wild idea)?


-tomer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20060708/74dcbdd6/attachment.htm 


More information about the Python-Dev mailing list