Anjith
Anjith

Reputation: 2308

convert timeit output into seconds or minutes

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

Answers (1)

U13-Forward
U13-Forward

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

Related Questions