Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'arguments': 0.07; 'decorator': 0.07; 'overflow': 0.07; 'caching,': 0.09; 'sep': 0.09; 'def': 0.10; 'subject:not': 0.11; 'sat,': 0.15; 'stack': 0.15; 'algorithm.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'personally,': 0.16; 'prefer.': 0.16; 'wrote:': 0.17; 'sort': 0.21; 'received:209.85.214.174': 0.21; '15,': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'trouble': 0.28; 'though.': 0.29; 'help,': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'clear': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'easy': 0.60; 'first': 0.61; 'skip:n 10': 0.63; 'as:': 0.75; 'algorithm,': 0.84; 'andrea': 0.84; 'fibonacci': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=V8JQWvbkKm3KaL1hm59WhKxr4XbOwgKMIkH0OJ50fUI=; b=frTu//bDWPobAWH0WXiMx+WPSZ6enEq0LAzkHSDIIDqcpC7FhQF1N+z64sW4+QUFD2 gmum6X3GJtTxg6xjA7bZ3QUPtZEN11iyrTsn+DcgmOyagu3V8JLLQIWL641jyq1ml0Mj stSARDjNbr8rei9UL52B21SqN4pPIBUZagzXq8erkq+Y3cSxM5+6r9lez9ebj6OlOz1l XM2xrhqy6100voMjL5VImeDG8TrlyYEarYiuuiGk2VzHtNmSx/1QLFXvtrIp27uHzCiL npXTEgl8kkEsNa3aA9fGZejIfMNqHRZNDQBnvSCGTQ3hpB9KymXqIUMvIEYsMOwQMO1Z CCOg== MIME-Version: 1.0 In-Reply-To: References: <1017333532.1009581.1347628946603.JavaMail.root@sequans.com> Date: Sat, 15 Sep 2012 00:41:01 +1000 Subject: Re: Decorators not worth the effort From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 1347633665 news.xs4all.nl 6890 [2001:888:2000:d::a6]:45038 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29168 On Sat, Sep 15, 2012 at 12:12 AM, andrea crotti wrote: > def fib(n): > if n <= 1: > return 1 > return fib(n-1) + fib(n-2) > > @memoize > def fib_memoized(n): > if n <= 1: > return 1 > return fib_memoized(n-1) + fib_memoized(n-2) > > > The second fibonacci looks exactly the same but while the first is > very slow and would generate a stack overflow the second doesn't.. Trouble is, you're starting with a pretty poor algorithm. It's easy to improve on what's poor. Memoization can still help, but I would start with a better algorithm, such as: def fib(n): if n<=1: return 1 a,b=1,1 for i in range(1,n,2): a+=b b+=a return b if n%2 else a def fib(n,cache=[1,1]): if n<=1: return 1 while len(cache)<=n: cache.append(cache[-1] + cache[-2]) return cache[n] Personally, I don't mind (ab)using default arguments for caching, but you could do the same sort of thing with a decorator if you prefer. I think the non-decorated non-recursive version is clear and efficient though. ChrisA