Language | Execution time(sec) |
---|---|
JS with V8 (Node 8.11) | 3.18 |
PHP 7.2 | 28.026075124741 |
PHP 7.0 | 28.537676095963 |
Ruby 2.5 | 37.355172 |
Python 2.7 | 70.2023770809 |
Python 3.6 | 99.59470009803772 |
PyPy 6 on Python 2.7 | 7.27390384674 |
FYI, Strictly-typed compile language
- C 1.3 sec
- Java 1.1 sec
- How to check version and run the REPL for each language
- node -v; node
- php -v; php -a
- ruby -v; irb
- python --version; python
const fib = (n) => {
if (n <= 1) {
return 0;
}
if (n === 2) {
return 1;
}
return fib(n - 2) + fib(n - 1);
}
const startTime = new Date().getTime();
const range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40];
range.forEach((i) => {
console.log(i, fib(i));
});
console.log((new Date().getTime() - startTime) / 1000);
// 3.18 @node v8.11.3
<?php // opcache.enable_cli => Off => Off
function fib(int $n) {
if ($n <= 1) {
return 0;
}
if ($n === 2) {
return 1;
}
return fib($n - 2) + fib($n - 1);
}
$startTime = microtime(true);
foreach (range(1, 40) as $i) {
echo $i, ' ', fib($i), PHP_EOL;
}
echo microtime(true) - $startTime;
// 28.026075124741 sec @PHP 7.2.10
// 28.537676095963 sec @PHP 7.0.30
def fib(n)
return 0 if n <= 1
return 1 if n == 2
return fib(n - 2) + fib(n - 1)
end
start_time = Time.now
(1..40).each do |i|
puts "#{i.to_s} #{fib(i).to_s}"
end
puts (Time.now - start_time)
# 37.355172 @ruby 2.5.0p0
import time
def fib(n):
if n <= 1:
return 0
elif n == 2:
return 1
return fib(n - 2) + fib(n - 1)
if __name__ == "__main__":
start_time = time.time()
for i in range(1, 40 + 1):
print(i, fib(i))
print(time.time() - start_time)
# 70.2023770809 sec @Python 2.7.10
# 99.59470009803772 sec @Python 3.6.4
#include <stdio.h>
#include <time.h>
int fib(int n) {
if (n <= 1) {
return 0;
}
if (n == 2) {
return 1;
}
return fib(n - 2) + fib(n - 1);
}
int main(void) {
clock_t start_time, end_time;
float exec_time;
start_time = clock();
for (int i = 1; i <= 40; i++) {
printf("%d %d\n", i, fib(i));
}
end_time = clock();
exec_time = (float)(end_time - start_time) / CLOCKS_PER_SEC;
printf("%.3f\n", exec_time);
return 0;
}
public class Fibonacci {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
for (long i = 1; i <= 40; i++) {
System.out.printf("%d %d \n", i, fib(i));
}
long endTime = System.currentTimeMillis();
long exeTime = endTime - startTime;
System.out.printf("%d ms\n", exeTime);
}
private static long fib(long n) {
if (n <= 1) {
return 0;
}
if (n == 2) {
return 1;
}
return fib(n - 2) + fib(n - 1);
}
}
Recursions are inherently heavy. In this scenario one function call may generate additional subsequent two function calls, if the parameter does not fall into the exit condition. When
fib(2)
orfib(1)
are called, the exit condition reaches and start to return.To depict it in a diagram

To get the 6th fibonacci number, there must be 15 function calls including the first call. And the number of function calls explode exponentially as the number grows.
The test was done on the same computer. Then, my next question is that where the difference come from? (Not an expert on computer internals though, I think there were no I/O with file, network, DB, etc...) Why Node and PyPy showed decent performance while others not?