Datetime with float seconds

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Aug 5 20:54:07 EDT 2009


En Wed, 05 Aug 2009 09:50:04 -0300, kpal <kpalamartchouk at gmail.com>
escribió:

> The standard datetime has 1 microsecond granularity.

Note that this is the finest granularity a datetime object can store, NOT
the precision of datetime.now() by example.

> My application
> needs finer time resolution, preferably float seconds.

"float seconds" doesn't mean much. time.time returns float seconds, but
its precision is around 15ms on Windows.

> Is there an
> alternative to the out-of-the-box datetime? Timezone support is not
> essential.

On Windows, use time.clock(), which internally uses
QueryPerformanceCounter. The resolution depends on your hardware, but it's
typically better than 1 microsecond. If you want to know the actual value:

py> from ctypes import *
py> kernel32=windll.kernel32
py> QueryPerformanceCounter=kernel32.QueryPerformanceCounter
py> QueryPerformanceFrequency=kernel32.QueryPerformanceFrequency
py> plonglong = POINTER(c_longlong)
py> QueryPerformanceFrequency.argtypes = [plonglong]
py> freq = c_longlong()
py> QueryPerformanceFrequency(byref(freq))
1
py> freq
c_longlong(3579545L)

That is, on my system the resolution is 1/3579545s, or better than 300ns

-- 
Gabriel Genellina




More information about the Python-list mailing list