6

I received the following script from a fellow programmer:

    from time import *
    start = strptime(asctime())
    end = strptime(asctime())
    print 'you took %i minutes' % (end[4] - start[4])

As you might already know, the script measures the time between the third and fourth line. However, it seems to measure it in minutes. How can I go about to measure it in seconds, with at least one decimal place (ex. 7.4 seconds).

Also, I would like to add one extra piece of functionality. Say the script runs, and I am asked by the program to type any word. At the end of my typing, I have to press the enter key to exit the program and measure the time from the first keystroke to the moment I press enter. How can I go about measuring this?

Nico Bellic
  • 363
  • 2
  • 4
  • 13

3 Answers3

22

First, I would avoid using import * as it's considered bad practice. You can use time.time() to get more precision:

>>> import time
>>> start = time.time()
>>> end = time.time()
>>> end - start
5.504057168960571

You could also use datetime.datetime.now().

jterrace
  • 64,866
  • 22
  • 157
  • 202
  • That worked fantastically. Thank you sir. As for the other part, how would I go about measuring time from the first keystroke to the time I press enter? – Nico Bellic Feb 03 '12 at 17:19
  • From first keystroke or from the time the prompt is presented? – jterrace Feb 03 '12 at 17:21
  • From the first keystroke to the time enter is pressed. – Nico Bellic Feb 03 '12 at 18:34
  • @NicoBellic I think that deserves a question of its own. I would edit this question and remove that part and start a new question. – jterrace Feb 03 '12 at 18:38
  • I opened a new question. Here it is: http://stackoverflow.com/questions/9133923/measuring-time-between-keystrokes-in-python . Also, thanks again for your help. – Nico Bellic Feb 03 '12 at 18:53
2
#source: http://docs.python.org/library/timeit.html
def test():
    """Stupid test function"""
    L = []
    for i in range(100):
        L.append(i)

if __name__ == '__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()
cetver
  • 11,279
  • 5
  • 36
  • 56
  • Hi cetver. I just tried your code and I feel like it's not working right. After I run the script, I write something, press enter, and after some time, the script outputs a float (which seems to be random). The floats are anywhere between 20 seconds to 40 seconds. No matter what the time of first stroke is, that is all I keep getting. – Nico Bellic Feb 03 '12 at 17:54
  • 1
    @NicoBellic that's because it runs it multiple times. See the docs for the timeit module. – jterrace Feb 03 '12 at 17:58
1

If you are trying to optimize a python web service call, you can do the following.

import time 

In the beginning of the function, right

 start = time.time()

in the line put (540 is the line number),

    l540 = time.time()
    print("--------l541 ------------")
    print(l540 - start)

in the next line put (608 is the line number),

  l608 = time.time()
  print("-------- 609 ------------")
  print(l608 - l540)

You can add as many as you want and it will tell you where exactly the program is taking time.