0

I did a small experiment of running a fibonacci function both in python and javascript. The time taken has a huge difference.

Python

import time

def fib(n):
    if n <= 1:
        return n
    else:
        return fib(n-1) + fib(n-2)


if __name__ == "__main__":
    start_time = time.time()
    fib(40)
    time_taken = time.time() - start_time
    print(f"Time taken = {time_taken}s")

enter image description here

Javascript

function fib(n) {
    if (n <= 1) {
        return n;
    } else {
        return fib(n-1) + fib(n-2);
    }
}


console.time('fibonacci');
fib(40);
console.timeEnd('fibonacci');

enter image description here

I am trying to understand the reason for such a huge difference in performance. Is it because of the GIL? If yes please add an explanation.

trincot
  • 317,000
  • 35
  • 244
  • 286
Ajitesh Singh
  • 401
  • 2
  • 5
  • 14

0 Answers0