What exactly are fractional seconds returned by Python time.perf_counter()?

Question:

This is probably a dumb/obvious question, but just want to make sure my hunches are correct.

I’m doing some basic performance timing in a Python3 script using time.perf_counter() like so:

start = time.perf_counter()
# some time consuming operation here
end = time.perf_counter()
elapsed = end - start

And I’ll get back values like 9.774 or 36.903 (to many more decimal places, of course). I’m assuming that larger numbers = more time elapsed, but what exactly do these numbers mean? E.g. is 1.23 fractional seconds just 1 second and .23 fractions of a second

Asked By: baisang

||

Answers:

As far as I know, “fractional second” just means a second with a fractional part (as opposed to a strictly integer number of seconds). So 9.774 means 9 seconds plus 774/1000 seconds.

Answered By: ezig

Additional info, for very small elapsed time it will return scientific notation like 3.6564000765793025e-05 then to format it to second and Milisecond/Microsecond

print(f'{elapsed:.3f} - Second & MiliSecond')
print(f'{elapsed:.6f} - Second & Microsecond')

# 0.000 - Second & MiliSecond
# 0.000037 - Second & Microsecond
Answered By: uingtea
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.