formatting time as milliseconds in logging module

Bengt Richter bokr at oz.net
Tue Oct 19 14:08:56 EDT 2004


On Tue, 19 Oct 2004 09:47:30 -0700, Josiah Carlson <jcarlson at uci.edu> wrote:

>> The logging module nicely prepends each line with a formatted date.
>> 
>> However, I'm not interested in the actual date but only in the number of 
>> milliseconds that passed since the start of the program. What is the best 
>> way of doing that?
>
>In Windows: time.clock() will get you the number of seconds since you
>last called it, down to the microsecond (trim as you see fit).
 ^^^^ first, it seems (certainly not a delta since the last time.clock() call)

 >>> if 1: #postpone interactive start of suite
 ...     print 'starting'
 ...     import time
 ...     time.sleep(5)
 ...     print time.clock()
 ...     for i in xrange(10):
 ...         print time.clock()
 ...         time.sleep(1.0)
 ...
 starting
 1.25714266558e-005
 0.00067466656386
 1.00140967598
 2.00285203766
 3.00429188506
 4.00575352293
 5.00716152272
 6.0086038844
 7.01003954133
 8.01151542682
 9.01291420756

Don't know why the first value is not a pure 0.0 though.

>
>In linux, time.clock() gives you the amount of processor time used, so
>it is not quite as useful in this case.
>
>
>In both, time.time() will give you the number of seconds since the epoch,
>so you can call it once when the program starts, and later just
>subtract the times.  In Windows, it is only accurate to .01 seconds, and
Maybe not even all windows. E.g., for NT that is one scheuling time slice,
but oher variants might have other granularity, IWT.

>I don't know how accurate the linux version is (I expect about as
>accurate, but I can certainly be wrong).
>
Unless your process is running with hard real time priority, you have to throw in
os multitasking and interrupt processing latency as well (try using the mouse to drag
a complex GUI window of another app around continuously while your test runs ;-).

Regards,
Bengt Richter



More information about the Python-list mailing list