Python Forum

Full Version: time travaller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am python newbie.

I would like to write a little code to keep track of a loop's start tim end end end time in seconds
pseudo code
import time
g_start_time = time.time() or time.clock()??? #store as global

while time:
    #do something
    end_time = time.time() or time.clock()??? #store as local
    if endtime - g_start_time == 4 #after loop excuted for 4 seconds
        #do foo
    else 
        #do bar
does this make sense? Als I am a bit confused the use of time.clock() and time.time(), in this case seems time.clock()maks more sense. Am I correct? Thanks
i woudl suggest using the timeit module
time.time() or time.clock()
This will always use time.time() because the value is never False (0.0).
You set start and end time then subtract.
This way only make sense for longer running loop.
timeit as mention which is made for measuring execution times.
import time

start = time.clock()
for i in range(5):
    time.sleep(i)
end = time.clock()
print(f'Code time {end - start:.2f} seconds')
Output:
Code time 10.01 seconds