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:
| Python | |
|---|---|
| 1 2 3 4 |  | 
How it appears in the terminal:
% Elapsed time: 0.12345 seconds
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:
| Python | |
|---|---|
| 1 2 3 4 |  | 
| Python | |
|---|---|
| 1 2 3 4 5 6 7 8 |  | 
In both cases, how it appears in the terminal is the same:
% Elapsed time: 0.12345 seconds
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 | |
|---|---|
| 1 2 3 4 5 6 7 8 |  | 
How it appears in the terminal:
% Elapsed time: 0.123456 seconds
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 | |
|---|---|
| 1 2 3 4 5 6 7 8 |  | 
How it appears in the terminal:
% Elapsed time: 0.123456789 seconds
This works both with or without the with statement for context management:
| Python | |
|---|---|
| 1 2 3 4 5 6 7 |  | 
| Python | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |  | 
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 APrecision in Nanoseconds
Timer for Python uses the native time.perf_counter_ns() function for maximum resolution in nanoseconds.