Recursive Fibonacci solution in different programming languages

Fibonacci Spiral

Once upon a time in the evening in one programmer chat, some programmers felt boring. And I don’t remember why, but we started to compare programming languages performance using the Fibonacci in its recursive implementation. Many times passed since it happened. Only artifacts such as source code left on my hard drive and that’s why I decided to create this post.

Short Fibonacci number sequence description: F[n] = F[n-1] + F[n-2] where starting point could be F[0] = 0 and F[1] = 1 or F[1] = 1 and F[2] = 2.

Here is math description:
Here is different programming languages implementations:

Let’s begin with PHP 7. Just because it’s my main language currently. And also it would be weaker one in speed with a recursive . For example, Javascript works much faster with a lot of functions calls by its nature. But Python 2.7 will be just a bit faster than PHP 7. These two languages stronger in different situations. That’s why objective and absolute performance of languages isn’t a purpose of this post. The purpose of this post is fun and interest, nothing more.

Time measurement will be done in a  just with build in command “time“.



I have used 35 as an argument. Just because PHP implementation works too long with argument value 40. But I will use 40 where it’s possible.

For PHP 5.6 results are slightly different. I don’t see drastically improvement in PHP 7.1…


Python 2.7

Have used 37 as an argument for the same reason as with PHP. For number 38 timing will be 12s.

Python 3.5

Only one difference in code

And python3 is slower than python2. :)

Javascript Node 7.5.0

Js is pretty fast with recursion based solution by its nature as explained in the beginning.

Let’s try some compiled languages instead of interpreted.

Java 8

We need to compile it in class in order to run it.

Compiled beats any interpreted or script.

(gcc version 5.4)

Also, should be compiled.

It beats even Java implementation. Just great.

Well, let’s go deeper….

Assembler (NASM version 2.11.08)

Here I had problems to convert 32-bit version into 64-bit. I had a problem to run it then with nasm because hadn’t worked with assembly since university. That’s why I have created SO question and got some help here. So, the code is:

And the fastest timing of course!

Great, just great!

I will stop here. :) Maybe I would update this post later with versions and timing for some high-level languages like C# and Ruby and for some low level like C. Also, I’m interested in running functional languages like Haskel, Erlang and Scala.

A bit later I decided to add Ruby.

Ruby 2.3

Actually Ruby beats PHP and Python with argument value 39.