Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
time travaller
#1
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
Reply
#2
i woudl suggest using the timeit module
Recommended Tutorials:
Reply
#3
time.time() or time.clock()
This will always use time.time() because the value is never False (0.0).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020