Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29168
| 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 | <rosuav@gmail.com> |
| 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 | <CAF_E5JY6c+QfYGo9N=0p7HdSa6NX8oL-Hj9gR6=7TayQeOqmoA@mail.gmail.com> |
| References | <XnsA0CE7D6F43F18duncanbooth@127.0.0.1> <1017333532.1009581.1347628946603.JavaMail.root@sequans.com> <CAF_E5JY6c+QfYGo9N=0p7HdSa6NX8oL-Hj9gR6=7TayQeOqmoA@mail.gmail.com> |
| Date | Sat, 15 Sep 2012 00:41:01 +1000 |
| Subject | Re: Decorators not worth the effort |
| From | Chris Angelico <rosuav@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.705.1347633665.27098.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Sat, Sep 15, 2012 at 12:12 AM, andrea crotti <andrea.crotti.0@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Decorators not worth the effort Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-09-14 11:28 +0200
Re: Decorators not worth the effort Duncan Booth <duncan.booth@invalid.invalid> - 2012-09-14 11:26 +0000
Re: Decorators not worth the effort andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-14 15:12 +0100
Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-15 00:41 +1000
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-14 09:37 -0700
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-14 09:37 -0700
Re: Decorators not worth the effort andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-14 17:15 +0100
Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-15 03:30 +1000
Re: Decorators not worth the effort Neil Cerutti <neilc@norwich.edu> - 2012-09-18 13:19 +0000
Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-18 23:25 +1000
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 08:14 -0700
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 08:14 -0700
RE: Decorators not worth the effort "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-14 23:14 +0000
Re: Decorators not worth the effort Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-14 12:01 +0000
Re: Decorators not worth the effort Tim Chase <python.list@tim.thechases.com> - 2012-09-14 08:06 -0500
Re: Decorators not worth the effort Steve Howell <showell30@yahoo.com> - 2012-09-14 18:13 -0700
Re: Decorators not worth the effort Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-09-14 13:49 +0200
csiph-web