Is there something wrong with this python code, why does it run so slow compared to ruby?

后端 未结 5 769
孤城傲影
孤城傲影 2021-01-13 07:54

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

5条回答
  •  悲哀的现实
    2021-01-13 08:30

    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
    

提交回复
热议问题