Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89528

Re: Wrote a memoize function: I do not mind feedback

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!news.panservice.it!feed.xsnews.nl!border01.ams.xsnews.nl!feeder01.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F09-10.ams.news.kpn.nl
From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: Wrote a memoize function: I do not mind feedback
Organization Decebal Computing
References <87h9s1zoap.fsf@Equus.decebal.nl> <mhlh2f$plv$2@ger.gmane.org> <mailman.49.1430167261.3680.python-list@python.org> <87y4lbxyc2.fsf@Equus.decebal.nl> <mailman.74.1430291009.3680.python-list@python.org>
X-Face "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E
X-Homepage http://www.decebal.nl/
Date Wed, 29 Apr 2015 10:04:21 +0200
Message-ID <87d22nxsvu.fsf@Equus.decebal.nl> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:UIIhEdFpubSPOM+SHv0/lphmh1E=
MIME-Version 1.0
Content-Type text/plain
Lines 29
NNTP-Posting-Host 81.207.62.244
X-Trace 1430295270 news.kpn.nl 19524 81.207.62.244@kpn/81.207.62.244:33813
Xref csiph.com comp.lang.python:89528

Show key headers only | View raw


Op Wednesday 29 Apr 2015 09:02 CEST schreef Ian Kelly:

> On Wed, Apr 29, 2015 at 12:06 AM, Cecil Westerhof <Cecil@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:

It is not only performance wise, I find the code without the try also
better looking. But that is very personally I suppose.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Wrote a memoize function: I do not mind feedback Cecil Westerhof <Cecil@decebal.nl> - 2015-04-27 15:35 +0200
  Re: Wrote a memoize function: I do not mind feedback Peter Otten <__peter__@web.de> - 2015-04-27 16:28 +0200
  Re: Wrote a memoize function: I do not mind feedback Albert-Jan Roskam <fomcl@yahoo.com> - 2015-04-27 20:35 +0000
    Re: Wrote a memoize function: I do not mind feedback Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 08:06 +0200
      Re: Wrote a memoize function: I do not mind feedback Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 01:02 -0600
        Re: Wrote a memoize function: I do not mind feedback Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 10:04 +0200
          Re: Wrote a memoize function: I do not mind feedback Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 09:58 +0100
            Re: Wrote a memoize function: I do not mind feedback Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 13:52 +0200

csiph-web