Reputation: 2308
How can I achieve the time a program took to execute in python
%timeit sum([i for i in range(1000) if i % 3 == 0 or i % 5 == 0])
For that I got this output:
172 µs ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
But I want the time in seconds or minutes?
How can I check time in seconds or minutes for this code:
k = 0
n = 1000
for i in range(1, n):
if i % 3 == 0 or i % 5 == 0:
k += i
Upvotes: 2
Views: 1766
Reputation: 71610
Try using timeit
module:
import timeit
n= number of times
print(timeit.timeit(lambda: sum([i for i in range(1000) if i % 3 == 0 or i % 5 == 0]),number=n))
Upvotes: 1