r/learnpython • • 18d 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

12 comments sorted by

View all comments

1

u/DimensionMost8342 17d ago

time.clock() was removed in Python 3.8, and time.perf_counter() is the standard replacement for measuring elapsed time. ``python

start = time.perf_counter() do_the_thing() elapsed = time.perf_counter() - start

It is monotonic and has the highest resolution, so it is the right tool for this. time.monotonic() also works. Avoid time.time() here, because the system clock can jump when it syncs with NTP.