用户输入的持续时间

Luk*_*rau 0 python time duration input raw-input

我想知道用户输入我用 raw_input() 记录的输入需要多长时间。
即他们是否需要 1 秒或 10 秒才能在命令行上输入内容。

是否有一种既定的方法可以做到这一点,或者我是否需要发明自己的方法来做到这一点?

The*_*nse 5

如果您只需要第二个分辨率(不是毫秒/微秒),您可以将代码包围起来time.time()以获取开始/结束时间,然后减去。

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."
Run Code Online (Sandbox Code Playgroud)

如果您想要更高的分辨率,请查看此处提供的代码:python time(milli seconds)calculation