1

I want to time a block of code. Looking at the time module, there are calls for "process_time()" and "perf_counter()". In theory, since we want the delta of the start and end time of a process, either should be suitable, based on the descriptions in the documentation. I tried each of them, and got radically different results, that couldn't be accounted for by external influences in the way the process ran.

So I had the test code collect values from process_time() and perf_counter() during the same run, and compared them, and they are still "radically" different. Shouldn't the deltas be similar, no matter which function was used?

When timing the same process, I'd expect the result to be the same, given a bit of rounding error. But this is off by a power of 10.

A sample result:

rpn@R59  python [master ?] 0 $ ./testTiming.py
Returned Status: 200
Request (pc) took 0.218 sec.
Request (pt) took 0.038 sec.
rpn@R59  python [master ?] 0 $

The code being tested:

#! /usr/bin/env python3

import time

class Timer:
    def __enter__(self):
        self.start_pt = time.process_time()
        self.start_pc = time.perf_counter()
        return self

    def __exit__(self, *args):
        self.end_pt = time.process_time()
        self.end_pc = time.perf_counter()
        self.interval = self.interval_pc = self.end_pc - self.start_pc
        self.interval_pt = self.end_pt - self.start_pt


import requests

with Timer() as t:
    conn =requests.get('https://www.google.com')
    print("Returned Status: {}".format(conn.status_code))

print("Request (pc) took {:.03f} sec.".format(t.interval_pc))
print("Request (pt) took {:.03f} sec.".format(t.interval_pt))
Robert Nix
  • 69
  • 1
  • 7
  • [Understanding time.perf\_counter() and time.process\_time()](//stackoverflow.com/q/25785243) – 001 Jan 14 '19 at 15:49
  • I saw that link, and it explains the difference between the two values, but that does not explain the difference between the deltas of the beginning and ending values when they are collected from the two different sources. – Robert Nix Jan 14 '19 at 15:59
  • It does. You have a request - while the requests is running and fetching data and coming back there is nothing to do - so process_time() is low ... but the clock is still ticking while the data from google is fetched - I do not quite get why you assume they should be more similar? – Patrick Artner Jan 14 '19 at 16:14
  • 1
    One is "how much time was spent computing something" `process_time()` and one is "how much time has passed" `perf_counter()` – Patrick Artner Jan 14 '19 at 16:23
  • I could buy that... But trying it on a strictly CPU bound task, process_time() is still about double perf_counter(). Perf_counter() delta should be equivalent to "wall clock" time, and process_time() delta should be non-idle time, correct? But for the network task, perf_counter() delta is larger than process_time() delta, and for CPU bound, perf_counter() delta is smaller than process_time() delta. – Robert Nix Jan 14 '19 at 17:26

0 Answers0