I thought both measure the amount of time? But they return very different numbers and I don't understand what the documentation is saying. Can anyone elaborate?
1 Answers
time.clock()
gives you an elapsed amount of time. time.time()
gives you the wall clock time.
You can use time.time()
to communicate with others (including humans) about when something happened. time.clock()
only lets you measure how long something takes.
Generally speaking, you'd use time.clock()
when you want to measure timings, time.time()
to schedule something. To that end time.time()
has to be set correctly on your computer (to agree with the rest of your region as to what time it is now), but time.clock()
doesn't, it just counts seconds from an arbitrary point in time (usually when your computer started or when your process first used the function).
The exact behaviour of time.clock()
depends on your OS (it could just measure process time, excluding time sleeping, or it could measure time elapsed even when the process is inactive, it could go backwards if your system time is adjusted, etc).
For some use-cases this variability in exact behaviour isn't good enough, and as such it is deprecated in Python 3. There better options are available for either measuring performance or process time, see time.perf_counter()
and time.process_time()
.

- 1,048,767
- 296
- 4,058
- 3,343
-
2also `time.clock()` is machine dependent and is currently deprecated. – warownia1 Aug 09 '16 at 15:12
-
@warownia1: Python 3 has better options, yes. – Martijn Pieters Aug 09 '16 at 15:12
-
@MartijnPieters In that case why does the following return complete different answers? ```s = time.time() time.sleep(10) print((time.time()-s))``` and ```s = time.clock() time.sleep(10) print((time.clock()-s)) ``` @warownia1 what are the python 3 options? I just need to measure how long a function took to execute. – JRR Aug 09 '16 at 15:18
-
if you want to measure performance, use `timeit` module – warownia1 Aug 09 '16 at 15:20
-
@JRR: you used `time.sleep(10)`. It depends on your OS if `time.clock()` includes that time at all. I did include that info in my answer already. – Martijn Pieters Aug 09 '16 at 15:22