Python vs Ruby

Magnus Lie Hetland mlh at idi.ntnu.no
Sat Jan 27 19:15:54 EST 2001


"Yukihiro Matsumoto" <matz at zetabits.com> wrote in message
news:877l3h5a29.fsf at hoyogw.netlab.co.jp...
[...]
> double?  Fibonacci is mostly based on function calls

Not necessarily...

(By the way, your function wasn't altogether correct...)

> so that it's not the best field of Ruby.  But they give almost same
> performance result on my box.

If you want one that's in the "best field" of Ruby, you might want to
do something like this:

def fib(n):
    x, y, z = 1, 1, 1
    if n > 2:
        for i in range(n):
            x, y, z = y, z, x+y
    return z

(Here, fib(1) is the first fib)

And if you insist on using a recursive version, at *least* you should
use dynamic programming/memoization to avoid redundant revcursive
calls, or you will end up with an exponential running time...
That would cut down on the number of function calls and perhaps
help wrt. Ruby... I don't know :-)

--

  Magnus Lie Hetland      (magnus at hetland dot org)

 "Reality is what refuses to disappear when you stop
  believing in it"                 -- Philip K. Dick






More information about the Python-list mailing list