[Python-Dev] exception too expensive? (original) (raw)
tomer filiba tomerfiliba at gmail.com
Sat Jul 8 18:49:46 CEST 2006
- Previous message: [Python-Dev] second draft of sandboxing design doc
- Next message: [Python-Dev] exception too expensive?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 trylookup(k): ... try: ... return b[k] ... except KeyError: ... return 17 ... def iflookup(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(trylookup, 56, 1000000) 0.6099998950958252 test(trylookup, 56, 1000000) 0.60899996757507324 test(trylookup, 56, 1000000) 0.6099998950958252
~0.61 sec
if_lookup with valid key
test(iflookup, 56, 1000000) 0.68799996376037598 test(iflookup, 56, 1000000) 0.68700003623962402 test(iflookup, 56, 1000000) 0.67200016975402832
~0.68 sec
try_lookup with invalid key
test(trylookup, 9456, 1000000) 7.062000036239624 test(trylookup, 9456, 1000000) 6.812000036239624 test(trylookup, 9456, 1000000) 6.8440001010894775
~6.90 sec
if_lookup with invalid key
test(iflookup, 9456, 1000000) 0.625 test(iflookup, 9456, 1000000) 0.64100003242492676 test(iflookup, 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
- Previous message: [Python-Dev] second draft of sandboxing design doc
- Next message: [Python-Dev] exception too expensive?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]