On Wed, Apr 29, 2015 at 12:06 AM, Cecil Westerhof <ce...@decebal.nl> wrote: > Op Monday 27 Apr 2015 22:35 CEST schreef Albert-Jan Roskam: >> def some_func(arg, _memoize={}): >> try: >> return _memoize[arg] >> except KeyError: >> result = some_expensive_operation(arg) >> _memoize[arg] = result >> return result > > That is in a way the same as what I do, except I do not use an > exception. Iunderstand it is not as expensive as it was anymore, but I > do not like to use an exception (when not necessary).
It's useful to keep in mind which case it is that you're trying to optimize. The expensive case for exceptions is when one actually gets raised. A try that doesn't raise an exception is pretty cheap, probably cheaper than looking up the key in the dict twice as the code you linked does. Compare: >>> from timeit import timeit >>> timeit("if x in y: y[x]", setup="x = 1; y = {1: 2}") 0.2224265851985905 >>> timeit(""" ... try: ... y[x] ... except KeyError: ... pass ... """, setup="x = 1; y = {1: 2}") 0.15237198271520924 On the other hand, if the KeyError does get raised, then it will be more expensive, but that would already be the expensive case for memoized function, and if that computation isn't significantly more expensive than the cost of raising an exception, then it probably isn't worth memoizing in the first place. Regardless of which way you write it, this sort of micro-optimization hardly ever matters, and in the situations where it does matter, you'll gain much more performance by rewriting in C than by obsessively tuning your Python code. -- https://mail.python.org/mailman/listinfo/python-list