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))