[Tutor] Time - Less then a second

exnihilo at myrealbox.com exnihilo at myrealbox.com
Thu Jan 8 22:36:53 EST 2004


Isr Gish wrote:
> I'm looking to be able to get time in less then seconds.
> I have looked in the time module and seen the time.clock() and time.time() functions. But on my machine (Pocket PC running Python Ver. 2.3.2) it doesn't return anything less than seconds. And they both return the same thing, (the full time since ??? In seconds).
> 
> If someone can please point me in the right direction it would be greatly appreciated.
> 
> (I have used ticks() previously in PocketC for the Windows CE platform. I have never programmed in any other language before besides PocketC) 
> 
> ---------------
> Isr Gish

hello again,

After playing around a bit, I'm now thoroughly confused.

 >>> help(time.clock)
Help on built-in function clock:

clock(...)
     clock() -> floating point number

     Return the CPU time or real time since the start of the process or 
since
     the first call to clock().  This has as much precision as the system
     records.


This indicates that the float will be as precise as the system records, 
yet time.time() seems to give me much greater precision:


 >>> def timeSort(l):
	start = time.time()
	l.sort()
	print "Sort Time:", (time.time() - start) * 1000, "milliseconds"

	
 >>> def clockSort(l):
	start = time.clock()
	l.sort()
	print "Sort Time:", (time.clock() - start) * 1000,"milliseconds"

	
 >>> tList = [random.random() for i in range(100000)]
 >>> cList = [random.random() for i in range(100000)]
 >>> timeSort(tList)
Sort Time: 184.044003487 milliseconds
 >>> clockSort(cList)
Sort Time: 200.0 milliseconds

And a second time:
 >>> tList = [random.random() for i in range(100000)]
 >>> cList = [random.random() for i in range(100000)]
 >>> timeSort(tList)
Sort Time: 207.070946693 milliseconds
 >>> clockSort(cList)
Sort Time: 180.0 milliseconds


Time certainly looks more precise than clock to me.

Okay, I checked the docs, as I should have done originally 
(http://python.org/doc/current/lib/module-time.html):

clock(   )

On Unix, return the current processor time as a floating point number 
expressed in seconds. The precision, and in fact the very definition of 
the meaning of ``processor time'', depends on that of the C function of 
the same name, but in any case, this is the function to use for 
benchmarkingPython or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the 
first call to this function, as a floating point number, based on the 
Win32 function QueryPerformanceCounter(). The resolution is typically 
better than one microsecond.

time(  	)
     Return the time as a floating point number expressed in seconds 
since the epoch, in UTC. Note that even though the time is always 
returned as a floating point number, not all systems provide time with a 
better precision than 1 second. While this function normally returns 
non-decreasing values, it can return a lower value than a previous call 
if the system clock has been set back between the two calls.

Hmmm, perhaps more experienced hands can explain the results of my tests.




More information about the Tutor mailing list