time in milliseconds by calling time.time()

Roy Smith roy at panix.com
Sat Jul 25 03:27:10 CEST 2009


In article 
<9c600f0c-f4a0-4e8c-bbb9-27f128aecc50 at m7g2000prd.googlegroups.com>,
 "scriptlearner at gmail.com" <scriptlearner at gmail.com> wrote:

> I am trying to measure some system response time by using the time.time
> () or time.clock() in my script.  However, the numbers I get are in
> 10s of milliseconds.
> [...]
> The tricky thing is, if I run the python interpreter and import the
> time module, I can get a time floating number in better precision by
> calling time.time().
> [...]
> >>> time.time()
> 1248481930.8023829  <--I like this!

time.time() is returning a float in either case.  The difference you are 
seeing is purely related to how you are printing it; executing a "print" 
statement as opposed to the interactive interpreter printing a value.

Notice:

Roy-Smiths-MacBook-Pro:play$ python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print time.time()
1248484949.75
>>> time.time()
1248484957.151274

and further notice:

>>> x = time.time()
>>> str(x)
'1248485028.58'
>>> repr(x)
'1248485028.5814769'

Keep in mind that while a float may have a large apparent precision, 
there's no promise that the actual value returned by the OS has that much 
precision.  You should be fine if all you're looking for is ms, but I 
wouldn't count on much more than that.



More information about the Python-list mailing list