Repository navigation
Best way to capture short spans in a Duration? #878
Replies: 1 comment
|
You can pass the floating point elapsed seconds directly to import pendulum
from time import perf_counter
start = perf_counter()
# Run the work being measured here.
elapsed_d = pendulum.duration(seconds=perf_counter() - start)
print(
f'{elapsed_d.in_hours()}:'
f'{elapsed_d.minutes:02}:'
f'{elapsed_d.remaining_seconds:02}.'
f'{elapsed_d.microseconds:06}'
)The fractional seconds are retained to microsecond precision. Including I tested Pendulum 3.2.0 with |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
i was to measure execution time with perfcounter and convert that to a Duration to take advantage of the attributes in formatting a logging message.
It seems like to do this one has to do roughly:
This seems a little convoluted, but conversely using
Datetime.now()doesn't capture any time elapsed for relatively fast intervals, so I'm not sure of a better way to achieve this.Edit-
For context this is for wrapping around some data processing functionality, so a large dataset may conceivably take a few hours but a test case or a small dataset may take virtually no time, hence the interest in easy access to the Duration's hour minute and second attributes while also capturing small spans of time as well.
All reactions