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


Groups > comp.lang.python > #89526

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

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!feeder2-2.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; 'cest': 0.09; 'exception,': 0.09; 'exception.': 0.09; 'function,': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; '(when': 0.16; 'cheap,': 0.16; 'dict': 0.16; 'expensive,': 0.16; 'keyerror': 0.16; 'matters,': 0.16; 'necessary).': 0.16; 'raised,': 0.16; 'raised.': 0.16; 'rewriting': 0.16; 'tuning': 0.16; 'exception': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'wed,': 0.18; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'regardless': 0.24; 'sort': 0.25; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'exceptions': 0.31; 'keyerror:': 0.31; 'probably': 0.32; 'skip:_ 10': 0.34; 'except': 0.35; 'monday': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'raising': 0.36; 'useful': 0.36; 'performance': 0.37; 'to:addr :python-list': 0.38; 'does': 0.39; 'expensive': 0.39; 'to:addr:python.org': 0.39; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'more': 0.64; 'linked': 0.65; 'worth': 0.66; 'gain': 0.79; '2015': 0.84; 'compare:': 0.84; 'hardly': 0.84; 'hand,': 0.93; 'anymore,': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=2vxcLrJdAcKPxbHJITfdBSEt4j+0zp/uPFLZixoOzEQ=; b=ntIJIa3gxbY49c3qyDuUgbxsDnD7bTs0s6agcksXfAUNnqTZ1ghnoih//oZDdX47PQ wH5CaBRtvUUJBOiqOYcwa4SZk/2uu784japz2Vg4e5/I+0hOeig4nzhzFSAsbUPxKA0a c0f2tAlajoR+RwdHexIaZP0T41YfHfkTgnrrSXGuuJPvDhjTiFl6z+mWPSgTlaxnd4dn Ik1L8pOY+N7iHr0e1qW0zJv6Cr9Da8LqKqePCk8+V14SXv9c91fU+VDl1AhgT/iGaHvh R/EsLHbuBVJojQ4OGr9VmL8YomzMgSdYkliQW94M81AETPXn93DM4qwWdHucSLxleBgI Y1AQ==
X-Received by 10.42.167.8 with SMTP id q8mr1892783icy.94.1430291000114; Wed, 29 Apr 2015 00:03:20 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <87y4lbxyc2.fsf@Equus.decebal.nl>
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>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 29 Apr 2015 01:02:39 -0600
Subject Re: Wrote a memoize function: I do not mind feedback
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.74.1430291009.3680.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430291009 news.xs4all.nl 2841 [2001:888:2000:d::a6]:41339
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89526

Show key headers only | View raw


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:

>>> 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.

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