Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Decorators not worth the effort

Reply
Thread Tools

Re: Decorators not worth the effort

 
 
Chris Angelico
Guest
Posts: n/a
 
      09-14-2012
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
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      09-14-2012
Chris Angelico於 2012年9月14日星期五UTC+8下午10時41分06 寫道:
> 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


Uhn, the decorator part is good for wrapping functions in python.

For example a decorator can be used to add a layor of some
message handlings of those plain functions
to become iterators which could be used as call back functions
in a more elegant way.



 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      09-14-2012
Chris Angelico於 2012年9月14日星期五UTC+8下午10時41分06 寫道:
> 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


Uhn, the decorator part is good for wrapping functions in python.

For example a decorator can be used to add a layor of some
message handlings of those plain functions
to become iterators which could be used as call back functions
in a more elegant way.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Decorators not worth the effort Jean-Michel Pichavant Python 4 09-15-2012 01:13 AM
Re: Decorators not worth the effort andrea crotti Python 0 09-14-2012 04:15 PM
Re: Decorators not worth the effort Jean-Michel Pichavant Python 0 09-14-2012 03:35 PM
Re: Decorators not worth the effort Jean-Michel Pichavant Python 1 09-14-2012 03:19 PM
Re: Decorators not worth the effort andrea crotti Python 0 09-14-2012 02:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57