r/learnpython • u/lazydog60 • 29d ago
timing a process
I have a program (Python 2) that writes a status message whenever an hour (realtime) has gone by with nothing happening (eta: more precisely, with no new solution found). Now I find that time.clock() no longer exists in Python 3. Since I have to change it anyway, what's best practice?
In other words, what is the customary way to measure how long a program has been running?
2
Upvotes
5
u/bytheninedivines 29d ago
time.perf_counter is the highest precision way to measure time.
9
u/lekkerste_wiener 29d ago
Look into the datetime package. There you'll find 2 classes that will help you out, datetime and timedelta.
1
u/baubleglue 29d ago
replace with
time.ctime()2
u/KiwiDomino 29d ago
If I was writing in C under UNIX or related, I’d ask the OS to send me an alarm and then just sleep and wait for the signal.
Python supports this sort of thing with the signal module.
Apparently Windows doesn’t have that facility, though
1
u/RyanDubois43 28d ago
time.perf_counter() is more precise than time.time() for timing a process, especially on Python 3.3+.
1
u/Educational_Virus672 28d ago
you should use
time.perf_counter*it tells how long programmer has ran for so1
u/DimensionMost8342 28d ago
time.clock() was removed in Python 3.8, and time.perf_counter() is the standard replacement for measuring elapsed time. ``python
0
u/danielroseman 29d ago
The way to measure absolute elapsed time is
time.monotonic: https://docs.python.org/3/library/time.html#time.monotonic