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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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?
From first keystroke or from the time the prompt is presented?
From the first keystroke to the time enter is pressed.
@NicoBellic I think that deserves a question of its own. I would edit this question and remove that part and start a new question.
I opened a new question. Here it is: stackoverflow.com/questions/9133923/… . Also, thanks again for your help.
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()

2 Comments

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.
@NicoBellic that's because it runs it multiple times. See the docs for the timeit module.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.