views:

267

answers:

5

I was interested in comparing ruby speed vs python so I took the simplest recursive calculation, namely print the fibonacci sequance.

This is the python code

#!/usr/bin/python2.7                       
def fib(n):
    if n == 0: 
        return 0
    elif n == 1:
        return 1 
    else:
        return fib(n-1)+fib(n-2)

i = 0
while i < 35:
    print fib(i)
    i = i + 1

and here is the ruby code

#!/usr/bin/ruby

def fib(n)
    if n == 0
        return 0
    elsif n == 1
        return 1
    else
        fib(n-1)+fib(n-2)
    end
end 

i = 0 
while (i < 35)
    puts fib(i)
    i = i + 1 
end

over several runs, time reports this average real 0m4.782s user 0m4.763s sys 0m0.010s thats for ruby, now python2.7 gives

real 0m11.605s user 0m11.563s sys 0m0.013s

Whats the deal?

+2  A: 

So for this code, Python is a bit more than two times slower than Ruby. Probably for other codes, Python will be faster than Ruby.

Your implementation of fib() has exponential run time. This can easily be avoided by using a loop. Python example:

a, b = 1, 1
for i in range(35):
    a, b = b, a+b
print b
Sven Marnach
The question is not how to write efficient fib() implementation. The question is why creating huge call stacks is more costly in Python (if it is) than in Ruby.
J.F. Sebastian
My main point actually should have been that the observed difference is small -- it is only a factor of two, which is not surprising at all. Reading my post again, I think other people's comments are better, but I will just leave it there :)
Sven Marnach
+2  A: 

Your method of calculating the first 35 numbers in the fibonacci sequence is immensely inefficient. You run a function fib() 35 times, and each time fib() has exponential run time. The generator in Python is the perfect solution to this problem and is far more efficient than what you wrote in Ruby.

def fibo_generator(n):
    # gets Fibonacci numbers up to nth number using a generator
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

You can then print all fibonacci numbers up to 35 using this code:

for f in fibo_generator(35):
    print f

This is by far the most efficient way to implement the fibonacci sequence in Python as well as the most versatile.

Rafe Kettler
I dont want the most effiecient for programming language x, I want to know why the almost exact python and ruby program runs in very different time frames. Is the ruby version optimizing the code behind my back? While we are at it I think memoization would speed it up immensly.
AntonioP
See THC4k's answer for why this test doesn't mean anything. This doesn't mean that Python is slower than Ruby because your application doesn't make much sense. Why would you do something that's inefficient in any language? Python is fastest when you play to its strengths, as is Ruby.
Rafe Kettler
+2  A: 

I was interested in comparing ruby speed vs python

Microbenchmarks are a really bad way to compare languages, especially before you mastered both. If you want a benchmark that has any real world meaning then you need to put a lot of effort into it - or you google for "language shootout"

Here is a better comparison of Python and Ruby

THC4k
Why is this a bad way to compare languages? What is the reason for the speed of ruby compared to the almost the same code in python? I am aware one could use generators, memoization and such to give the sequence in better time... but what Im interested in - why this big difference for the same code?
AntonioP
@AntonioP: Because only beginners write code like that. Your benchmark tells you only that "this bad Python programs is slower than that bad Ruby program". It only makes sense to compare the fastest implementations, not one with the same code.
THC4k
@THC4k: I have to both agree and disgree at the same time. It is true that comparing bad programs isn't helpful. However, I disagree that you should compare the fastest implementations. I think you should compare the most well-designed implementations. It is the compiler writer's job to make sure that the most well-designed implementation is also the fastest, but if it *isn't*, then you shouldn't optimize the code, you should fix the compiler.
Jörg W Mittag
@Jörg W Mittag: Yeah I completely agree with you.
THC4k
@Jörg W Mittag: Different people are likely to have different opinions about which are the "most well-designed implementations".
igouy
+4  A: 

The recursion efficiency of python is the cause of this overhead. See this article for much more detail. The above solutions that solve this iteratively are better for python since they do not incur the function call overhead recursion does. My assumption about ruby would be that it is clearly optimizing the code while python is not. Again, that article goes into a lot detail about this using a nearly identical fib function.

dcolish
I like the comments on that article you linked too. This does shed some light on using recursive function calls in python.
AntonioP
+1  A: 

Here's some more numbers to compare:

Python2.7
9.67 user 0.09 system 0:09.78 elapsed 99%CPU (0avgtext+0avgdata 16560maxresident)k
0inputs+0outputs (0major+1169minor)pagefaults 0swaps

ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
28.37 user 0.35 system 0:28.78 elapsed 99% CPU (0avgtext+0avgdata 9200maxresident)k
1896inputs+0outputs (9major+656minor)pagefaults 0swaps

ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
6.21 user 0.08 system 0:06.36 elapsed 98% CPU (0avgtext+0avgdata 14160maxresident)k
4416inputs+0outputs (16major+953minor)pagefaults 0swaps

Python is three times faster than ruby1.8 and 30% slower than ruby1.9.1 for the code provided.

Other Python versions for comparison:

2.4.6 took 10.30 seconds

2.5.5 took 9.93 seconds

2.6.6 took 9.22 seconds

2.7   took 9.35 seconds

3.0.1 took 11.67 seconds

3.1.2 took 11.35 seconds

3.2a3+ (py3k:85895, Oct 29 2010, 01:41:57) 
[GCC 4.4.5] took 13.09 seconds

2.5.2 (77963, Oct 15 2010, 02:00:43)
[PyPy 1.3.0] took 21.26 seconds

2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54) 
[OpenJDK 64-Bit Server VM (Sun Microsystems Inc.)] took 8.81 seconds
J.F. Sebastian