If you find the comments more confusing than helpful, here's a non-commented version. # Obviously it won't run it more than 20 times if you use time.sleep(1) # On my machine, it ran the loop 2605326 times in 20 seconds. # but it will run a lot more times, and the process time will # It should still run for the correct number of seconds, # For funsies, try removing "time.sleep()", and see what happens.
# This is because we are spending most of the time sleeping, It is the passing seconds after the epoch - 1st January 1970, 00:00:00 (UTC). # You'll notice that the process time is almost nothing. In this tutorial, we will discuss various Python Timer functions using the time module. # You were sleeping in your original code, so I've stuck this in here. Print "loop cycle time: %f, seconds count: %02d" % (time.clock(), elapsed) # Read the documentation for more details. # subtracting the first value of time.clock() from anything. There is no point assigning it to a variable, or # When you first call time.clock(), it just starts measuring # value, then subtract the start from all following values. # To find the time since the start of the function, we get the start # time.time() returns the number of seconds since the unix epoch. (There may be another way to do it if you are using 2.x, but I don't know what it is.) #!/usr/bin/python
Python active timer modules upgrade#
(If you are using Windows, you may want to upgrade to Python 3.3 which introduces the platform independent function time.process_time(). We can still use time.clock() to determine the process time if we are using a *nix OS. However, on Windows, time.clock() returns the actual time since it was first called.Ī better way to determine the time since the function was first called is to use time.time(), which returns the number of seconds since the epoch.
Python active timer modules code#
timeit (stmtpass, setuppass, timerAccording to the documentation, time.clock() returns processor time, which basically means that any time spent in time.sleep() is not counted. Python Interface ¶ The module defines three convenience functions and a public class: timeit. It is implemented upon the standard Python sched module and is used in a similar way. I'm not sure this code does what you think it does, at least on a *nix OS. The timesched Python module provides a simple time event scheduler.