[Python-ideas] Add time.time_ns(): system clock with nanosecond resolution

Todd toddrjen at gmail.com
Mon Oct 16 12:13:10 EDT 2017

On Mon, Oct 16, 2017 at 3:58 AM, Victor Stinner <victor.stinner at gmail.com>

> Hi,
> > What if we had a class, say time.time_base.  The user could specify the
> base
> > units (such as "s", "ns", 1e-7, etc) and the data type ('float', 'int',
> > 'decimal', etc.) when the class is initialized. (...)
> It's easy to invent various funny new types for arbitrary precision.
> But I prefer reusing the standard Python int type since it's very well
> known and very easy to manipulate. There is not need to modify the
> whole stdlib to support a new type. Moreover, we don't have to
> "implement a new type".
I guess I wasn't clear.

I am not suggesting implementing a new numeric data type.  People wouldn't
use the class directly like they would an int or float, they would simply
use it to define the the precision and numeric type (float, int, decimal).
Then they would have access to the entire "time" API as methods.  So for
example you could do something like:

   >>> import time
   >>> ns_time_int = time.time_base(units='ns', type=int)
   >>> ms_time_dec = time.time_base(units=1e-6, type='Decimal')
   >>> s_time_float= time.time_base(units=1, type=float)  # This is
identical to the existing "time" functions
   >>> ns_time_int.clock()
   >>> ms_time_dec.clock()
   >>> s_time_float.clock()
   >>> ns_time_int.perf_counter()
   >>> ms_time_dec.perf_counter()
   >>> s_time_float.perf_counter()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171016/ae211fdc/attachment.html>

More information about the Python-ideas mailing list