Skip to content

Instantly share code, notes, and snippets.

@appkr
Last active April 16, 2022 06:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appkr/ced41a97796116bead6802d42002d3a0 to your computer and use it in GitHub Desktop.
Save appkr/ced41a97796116bead6802d42002d3a0 to your computer and use it in GitHub Desktop.
Performance Comparison for Dynamically-typed Non-compile Languages

Test Result

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

Test Env

  • How to check version and run the REPL for each language
    • node -v; node
    • php -v; php -a
    • ruby -v; irb
    • python --version; python

asciicast

Test Code

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);
    }
}
@appkr
Copy link
Author

appkr commented Apr 16, 2022

Use loop instead of recursion

  • while fibonacci(47) in java, implemented using recursion takes 7sec see the link
  • the same operation takes 0ms when implemented using loop see the link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment