python - python中的时间测量脚本

标签 python

我从一位程序员同事那里收到了以下脚本:

    from time import *
    start = strptime(asctime())
    end = strptime(asctime())
    print 'you took %i minutes' % (end[4] - start[4])

您可能已经知道,该脚本测量第三行和第四行之间的时间。但是,它似乎以分钟为单位进行测量。我怎样才能以秒为单位进行测量,至少保留一位小数(例如 7.4 秒)。

另外,我想添加一项额外的功能。假设脚本运行,程序要求我输入任何单词。在我打字结束时,我必须按 enter 键退出程序并测量从第一次击键到我按下 enter 的时间。我该如何衡量呢?

最佳答案

首先,我会避免使用 import *,因为它被认为是不好的做法。您可以使用 time.time()获得更高的精度:

>>> import time
>>> start = time.time()
>>> end = time.time()
>>> end - start
5.504057168960571

你也可以使用 datetime.datetime.now() .

关于python - python中的时间测量脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9132536/

相关文章:

python - 尝试在 Windows 上克隆 pypy 的 Mercurial 存储库

python - pyqt QTableView 删除行导致异常 : Qt attempts to create header for removed row

python - 使用 dict 键作为列名写入 CSV 文件

Python + Cronjob : Why doesn’t cronjob execute os. 系统()?

python - 如何将python程序完全转化为exe文件?

python - 如何在Python中创建嵌套矩阵

python - 如何将输入值与mysql数据库进行比较,没有匹配数据时自动添加 '0'和 '1'

python - 在用户输入的某些点切片和添加换行符的正确方法

python - 如何编写异常捕获代码在 Python2.4 到 Python3 中有效

python - 从网站上抓取不断变化的整数