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

后端 未结 5 799
孤城傲影
孤城傲影 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:31

    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.

提交回复
热议问题