0

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?

LukasKawerau
  • 971
  • 1
  • 17
  • 39

4 Answers4

5

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

Community
  • 1
  • 1
TheSoundDefense
  • 6,368
  • 1
  • 26
  • 40
1

You can also use the timeit module.

import timeit

def read_input():
    global in_str
    in_str = raw_input('Enter text: ')

in_str = ''

s = total_time = timeit.timeit('read_input()', number=1,
                               setup='from __main__ import read_input')

print(in_str)
print(s)

The s will be in seconds, but it has microsecond granularity on Windows and 1/60s on Linux.

jure
  • 374
  • 6
  • 8
0

you can use time.time() for this purpose

import time
start=time.time()
inp=raw_input(" enter the input")
print start-time.time()
sundar nataraj
  • 8,200
  • 2
  • 30
  • 44
0

You could use time.time() before and after the input, then just take the difference. The answer will be in seconds.

>>> import time
>>> t1 = time.time()
>>> s = raw_input("enter something")
hello
>>> t2 = time.time()
>>> enter_time = t2-t1
>>> enter_time
17.92899990081787
Cory Kramer
  • 107,498
  • 14
  • 145
  • 201