This is the code I'm trying to execute...I have imported and used the timeit module to check which function executes faster, fibonacci_recur
or fibonacci_iter
. I'm executing this python file in VS Code(version: 1.41.1(user setup))
#Importing timeit
import timeit
def fibonacci_recur(num):
if num<0:
print("Incorrect input")
elif num==0:
return 0
elif num==1:
return 1
else:
return fibonacci_recur(num-1)+fibonacci_recur(num-2)
def fibonacci_iter(num):
if (num == 0):
return 0
elif (num == 1):
return 1
elif (num >1 ):
fn = 0
fn1 = 1
fn2 = 2
for i in range(3, num):
fn = fn1+fn2
fn1 = fn2
fn2 = fn
return fn
else:
return -1
print("{:9}{:9}{:9}".format("Number","Iterative","Recursive"))
for i in range(10,35,5):
#Using the timeit module
("{:9}{:9}{:9}".format(i,timeit.timeit(fibonacci_iter, number=100000),timeit.timeit(fibonacci_recur, number=100000)))
This is the error I get while debugging:
Could not load source '<timeit-src>'
: Source unavailable.
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
]
}
What could be the problem and how should I fix this?
The required package was time
not timeit
import time
To find time elapsed for a function:
start = time.time()
recursive_fibonacci(n)
end = time.time()
print(end - start, 'seconds')