Reputation: 2122
I am trying to test how long a http request processing code block takes inside my flask controller, here is the sample code I used:
cancelled = []
t0 = time.time()
t1 = time.clock()
users = requests.get('https://www.example.com/users/')
for i in users.json():
user = requests.get('https://www.example.com/user/%s' % i['id]').json()
if user['status'] == 'Cancelled':
cancelled.append(user)
t2 = time.clock()
t3 = time.time()
print t2 - t1
print t3 - t0
Here are the outputs:
2.712326
76.424875021
The second output from the time.time() function matches the actual seconds it took to display the results, so I am not sure why the value from time.clock() is so small?
Edit: My system is OSX and python 2.7, and my question is that why is time.clock() generally considered "better" if time.time() reflects the actual time a user experiences/waits?
Upvotes: 1
Views: 76
Reputation: 16099
Note that as of Python 3.3 time.clock is now deprecated as the behavior is platform dependent. The documentation recommends using time.process_time or time.perf_counter for measuring performance.
Otherwise I would recommend using the timeit module (especially since this allows you much more control over the testing environment)
Upvotes: 1