Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29178
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <andrea.crotti.0@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'arguments': 0.07; 'decorator': 0.07; 'ugly': 0.07; 'caching,': 0.09; 'logic': 0.09; 'solution,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; 'algorithm.': 0.16; 'caching': 0.16; 'personally,': 0.16; 'prefer.': 0.16; 'then?': 0.16; 'mathematical': 0.17; 'saying': 0.18; 'sort': 0.21; 'cc:2**0': 0.23; 'example': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; 'trouble': 0.28; 'though.': 0.29; 'url:mailman': 0.29; 'definition': 0.29; "i'm": 0.29; 'maybe': 0.29; 'url:python': 0.32; 'help,': 0.32; 'could': 0.32; 'url:listinfo': 0.32; 'version': 0.34; 'received:google.com': 0.34; 'clear': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'talk': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'easy': 0.60; 'close': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'as:': 0.75; 'algorithm,': 0.84; 'iterative': 0.84 |
| 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 :cc:content-type; bh=6+S9HmfNtfWFUgMYadi+we5UoLElnY7YDGexygqSR/k=; b=y+K64uXEnwqYvzKiCBcAQOBcndThHDM2HhXNEnmy6T9lf9TetrbBbQY9mC48UfULGQ QCwTtS6O1wl7ftvyKVEDmeiQkJePa7Z7gQAIO8LuGC64z19sA45QuZ7Y1TpvjHQPTG2O jjoUNtJieow90LR9BKX5qKkoXFc/ye6cZzCwGtaPjECwJj6ZMFE9GVausr4BFvonnv+U H67YV55bLF79XhQyLLFaW0bxivIGj6xVG+6L/YzRGYoapEdXNcIts5Ow8cpwRqO01CLr x8D7ZCjJAI27P/6RIQAyNnOtyOqLqzVCgqsOAR3wrqUsMvwtME2G8n8bcrFRuPBxQUDB QH4A== |
| MIME-Version | 1.0 |
| In-Reply-To | <CAPTjJmoSt84L_kpKWSUPLUJ38wDVp8ukHovd1QkFYUuJAnoeRQ@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> <CAPTjJmoSt84L_kpKWSUPLUJ38wDVp8ukHovd1QkFYUuJAnoeRQ@mail.gmail.com> |
| Date | Fri, 14 Sep 2012 17:15:35 +0100 |
| Subject | Re: Decorators not worth the effort |
| From | andrea crotti <andrea.crotti.0@gmail.com> |
| To | Chris Angelico <rosuav@gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | python-list@python.org |
| 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.712.1347639338.27098.python-list@python.org> (permalink) |
| Lines | 38 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1347639338 news.xs4all.nl 6874 [2001:888:2000:d::a6]:49917 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:29178 |
Show key headers only | View raw
2012/9/14 Chris Angelico <rosuav@gmail.com>: > > 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 > -- > http://mail.python.org/mailman/listinfo/python-list The poor algorithm is much more close to the mathematical definition than the smarter iterative one.. And in your second version you include some ugly caching logic inside it, so why not using a decorator then? I'm not saying that with the memoization is the "good" solution, just that I think it's a very nice example of how to use a decorator, and maybe a good example to start with a talk on decorators..
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