How to calculate execution time in python

Free Python Code - Sep 5 '23 - - Dev Community

code


import time

def test():
    for i in range(100):
        print(i)

    time.sleep(1)


# starting time
start = time.time()

test()

# end time
end = time.time()

# total time taken
print(f"[Execution time is : {end - start} s] ")
Enter fullscreen mode Exit fullscreen mode

result

[Execution time is : 1.0190958976745605 s] 
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .