Reputation: 1071
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?
Upvotes: 1
Views: 3301
Reputation: 45
import time
start = time.time()
in_str = input("Enter the thing:")
end = time.time()
elapsed = end-start
print(elapsed)
If you use this it should give you the answer in and precisely to a long list of decimal points. @TheSoundDefence for most of this code.
Upvotes: 0
Reputation: 412
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.
Upvotes: 1
Reputation: 118001
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
Upvotes: 0
Reputation: 8702
you can use time.time() for this purpose
import time
start=time.time()
inp=raw_input(" enter the input")
print start-time.time()
Upvotes: 0
Reputation: 6945
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
Upvotes: 6