1

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?

Pig
  • 2,002
  • 5
  • 26
  • 42
  • 1
    Well time.clock() is the processor time if you are on unix, and time.time() is seconds since epoch – Jacobr365 Apr 10 '17 at 15:31
  • Dupliate: http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy – Alex Apr 10 '17 at 15:31
  • 1
    Possible duplicate of [Python - time.clock() vs. time.time() - accuracy?](http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy) – Jordan Lewis Apr 10 '17 at 15:31
  • What operating system? `time.clock` returns the "current processor time". The docs say "The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name" – mgilson Apr 10 '17 at 15:31
  • Why is time.clock() generally considered "better" if time.time() reflects the actual time a user experiences/waits? – Pig Apr 10 '17 at 15:36

1 Answers1

1

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)

Grr
  • 15,553
  • 7
  • 65
  • 85