How to Use Decimals for Precision#
To set the number of decimals in the output, use the decimals
parameter. Instead of the default value 2
for decimals
, you can set the output precision up to 9
in the decimals
parameter:
How it appears in the terminal:
Configuration Options#
Default value for decimals
is 2
. The range is minimum 0
for no decimals and up to 9
:
Decimals | Example Output |
---|---|
0 | 12 seconds |
1 | 12.3 seconds |
2 (default) | 12.34 seconds |
3 | 12.345 seconds |
... | ... |
9 | 12.345678901 seconds |
Humanised Output
When you measure time in microseconds, decimal precision may be important. But if a program runs for a minute or more, it doesn't make sense to display the output time in milliseconds. Therefore, the decimal configuration will be overridden in certain cases by humanised output.
Basic Usage#
To set the number of decimals in the output (only if less than an hour), use the decimals
parameter. Instead of the default value 2
for decimals
, you can set the output precision up to 9
in the decimals
parameter.
Both with or without the with
statement for context management:
In both cases, how it appears in the terminal is the same:
Different Decimals for Different Threads#
General Configuration#
It's also possible to set the decimals when initiating the Timer. This will last throughout the session (unless overridden by a specific thread):
Python | |
---|---|
How it appears in the terminal:
How to Bypass General Configuration and Set Decimals by Thread#
Or set the decimals when starting a new thread, which will also override the general decimals defined when initiating the Timer.
Python | |
---|---|
How it appears in the terminal:
This works both with or without the with
statement for context management:
In both cases, how it appears in the terminal is the same:
% Elapsed time: 0.12 seconds for thread B
% Elapsed time: 0.12345 seconds for thread A
Precision in Nanoseconds
Timer for Python uses the native time.perf_counter_ns()
function for maximum resolution in nanoseconds.