I want to capture timestamps with sub-second precision in python. It looks like the standard answer is int(time.time() * 1000)
However, if time.time() returns a float, won't you have precision problems? There will be some values that won't represent accurately as a float.
I'm worried about some fractional times that don't represent correctly as a float, and the timestamp jumping forward or backward in those cases.
Is that a valid concern?
If so, what's the work-around?
How much precision do you want? While it's true that there are finite decimal fractions that can't be represented as finite binary fractions, the nearest approximate value is going to round to the correct number of integer milliseconds as long as you aren't timing a program running for 143 millenia (2**52 milliseconds).
In short: I don't think you need to worry about floating-point precision for this. You might need to worry about system timer accuracy, precision, or monotonicity, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With