Search code examples
pythoninputtimedurationraw-input

Time duration of user input


I'd like to know how long it takes a user to enter an input that I record with raw_input().
I.e. does it take them 1 second or 10 seconds to enter something on the command line.

Is there an established way of doing this, or would I need to invent my own way of doing this?


Solution

  • If all you need is second resolution (not millisecond/microsecond), you can surround the code with time.time() to get the beginning/ending times, then subtract.

    import time
    
    start = time.time()
    in_str = raw_input("Enter the thing:")
    end = time.time()
    elapsed = end-start
    print "That took you " + str(elapsed) + " seconds. Man, you're slow."
    

    If you want it at a greater resolution, take a look at the code presented here: python time(milli seconds) calculation