The choice between time.time()
and time.perf_counter()
depends on the context in which you will use the function. That's because each function deals with a different "type of time".
time.time()
deals with absolute time, i.e., "real-world time" (the type of time we're used to). It's measured from a fixed point in the past. According to the docs, time.time()
returns:
(...) the time in seconds since the epoch as a floating point number.
time.perf_counter()
, on the other hand, deals with relative time, which has no defined relationship to real-world time (i.e., the relationship is unknown to us and depends on several factors). It's measured using a CPU counter and, as specified in the docs, should only be used to measure time intervals:
The reference point of the returned value is undefined, so that only
the difference between the results of consecutive calls is valid.
Because of that, time.perf_counter()
is mostly used to compare performance.
That said, I don't think there is any point in comparing the speed of those two functions. They were designed to be used for different things - you should pick the one that best suits your use case. Quoting a comment by @Martijn Pieters:
Sometimes you need a hammer, sometimes you need a screwdriver. You
don't ask if one or the other is faster; you either have a nail or a
screw. Trying to use a screwdriver on a nail may work,
but is not the best choice.
From what I've gathered from your comment, you're probably better off using time.perf_counter()
, since your focus is in relative time and not in "real-world time". In such a context, time.perf_counter()
is probably going to be more precise, because it uses:
(...) a clock with the highest available resolution to measure a short
duration.