[Python-Dev] Store timestamps as decimal.Decimal objects (original) (raw)
Victor Stinner victor.stinner at haypocalc.com
Wed Feb 1 09:03:35 CET 2012
- Previous message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Next message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
2012/2/1 Nick Coghlan <ncoghlan at gmail.com>:
The secret to future-proofing such an API while only using integers lies in making the decimal exponent part of the conversion function signature:
def fromcomponents(integer, fraction=0, exponent=-9): return Decimal(integer) + Decimal(fraction) * Decimal((0, (1,), exponent))
The fractional part is not necessary related to a power of 10. An earlier version of my patch used also powers of 10, but it didn't work (loose precision) for QueryPerformanceCounter() and was more complex than the new version. NTP timestamp uses a fraction of 2**32. QueryPerformanceCounter() (used by time.clock() on Windows) uses the CPU frequency.
We may need more information when adding a new timestamp formats later. If we expose the "internal structure" used to compute any timestamp format, we cannot change the internal structure later without breaking (one more time) the API.
My patch uses the format (seconds: int, floatpart: int, divisor: int). For example, I hesitate to add a field to specify the start of the timestamp: undefined for time.wallclock(), time.clock(), and time.clock_gettime(time.CLOCK_MONOTONIC), Epoch for other timestamps.
My patch is similar to your idea except that everything is done internally to not have to expose internal structures, and it doesn't touch decimal or datetime modules. It would be surprising to add a method related to timestamp to the Decimal class.
This strategy would have negligible performance impact
There is no such performance issue: time.time() performance is exactly the same using my patch. Depending on the requested format, the performance may be better or worse. But even for Decimal, I think that the creation of Decimal is really "fast" (I should provide numbers :-)).
Victor
- Previous message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Next message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]