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: 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> <87y4lbxyc2.fsf@Equus.decebal.nl> From: Ian Kelly Date: Wed, 29 Apr 2015 01:02:39 -0600 Subject: Re: Wrote a memoize function: I do not mind feedback To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Apr 29, 2015 at 12:06 AM, Cecil Westerhof 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.