Reputation: 2325
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?
Upvotes: 3
Views: 3247
Reputation: 8491
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.
Upvotes: 3