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);
}
}
I did this test to verify the authenticity of the following article.
https://hackernoon.com/django-vs-laravel-which-framework-should-you-choose-1d0f40049ec1
While the article benchmarks http transaction per second(tps) on framework, I've tested performance in language level. The reason being, there are so many optimization points in fully functioning web service, e.g., selection of web server, process manager, library, ..., and their settings. So I just wanted to know the baseline.