0

I've been trying this Python iterative code to get the Fibonacci sequence on Windows and Linux. The problem is that this code takes much longer to execute on Windows and I don't know why. Is it due to the memory management of Windows? Both OS use Python 3.

def fib(n):
     old = 0
     new = 1
     temp = 0

    for i in range(1,n):
        temp = new
        new = new + old
        old = temp

    return new

def main():
    a = 10000
    for i in range(1,51):
        start_time = time.time()
        fib(a)
        elapsed_time = time.time() - start_time
        print("%.10f" % elapsed_time)
        a = a+10000

The time is measured in seconds.

Time graph:

Time graph

Thank you.

UPDATE: cProfile on both OS

Linux

Windows

The "percall" time seems to be huge on Windows compared to Linux.

Danubio
  • 93
  • 8
  • You should use an actual profiler on both OS and see what is the bottleneck. FWIW, I realize this is not what this question is about, but you can use a formula to calculate the n-th Fibonacci number in O(1). – DeepSpace Oct 07 '18 at 09:54
  • Thanks, I've updated the information with two screenshots using cProfile. The difference between both OS is huge, but I don't know what's happening there. I guess is something related to the memory management and the execution plan. – Danubio Oct 07 '18 at 15:13

0 Answers0