Python Forum
time travaller - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: time travaller (/thread-4206.html)



time travaller - tony1812 - Jul-29-2017

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


RE: time travaller - metulburr - Jul-29-2017

i woudl suggest using the timeit module


RE: time travaller - DeaD_EyE - Jul-29-2017

time.time() or time.clock()
This will always use time.time() because the value is never False (0.0).


RE: time travaller - snippsat - Jul-29-2017

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