[Tutor] displaying clock and/or elapsed time
Steven D'Aprano
steve at pearwood.info
Tue May 11 16:46:05 CEST 2010
On Tue, 11 May 2010 11:54:13 pm Art Kendall wrote:
> I am learning python and I want to see how long parts of of a process
> take. Are there system variables that can just be displayed?
> how do I get variables that contain the wall time (x) and the elapsed
> time (y)?
Get the current time in seconds since the start of the universe (10am on
January 1st 1970 on Linux systems, it may be slightly different on
Windows or Macintosh):
>>> import time
>>> time.time()
1273588356.8070121
Get the current time in a more human-readable fashion:
>>> time.ctime()
'Wed May 12 00:33:22 2010'
Get the elapsed time:
>>> start = time.time() # save the current time
>>> for i in range(1000): # do some work
... pass
...
>>> elapsed = time.time() - start
>>> print "work took %f seconds" % elapsed
work took 0.015870 seconds
See the documentation for the time module:
http://docs.python.org/library/time.html
That's a bit technical and newbie-unfriendly, so you might also like to
read this:
http://effbot.org/librarybook/time.htm
although it's very old and parts of it are obsolete.
--
Steven D'Aprano
More information about the Tutor
mailing list