python - 计算特定操作后耗时

标签 python function loops time

我想计算自从两组变得相等以来的时间。我使用 time.time() 来计算时间。但每当我计算集合相等的时间时,它都会返回时间 1520763846.19。我想计算两组变得相等并且必须执行一些下游操作的时间。任何帮助将不胜感激?

dis_cars,forbid_cars 是两组

def cars_on_road(dis_cars,forbid_cars):
    if len(forbid_cars) > 0 and dis_cars == forbid_cars:
        time_calculate = time.time()
        print "time_calculate" + str(time_calculate)

最佳答案

def __init__(self):
    self.last = time.time() #Initialization instant

def cars_on_road(self, dis_cars,forbid_cars):
    if len(forbid_cars) > 0 and dis_cars == forbid_cars:
        time_calculate = time.time() - self.last #Difference between self.last and the instant the sets became equal
        print "time_calculate" + str(time_calculate)
        self.last = time.time() #Do this if this method will be called again

关于python - 计算特定操作后耗时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49219009/

相关文章:

c++ - 在 C++ 中描述什么是虚函数的最简洁但准确的方法是什么?

postgresql - 将laSTLogin保存在表中

python - 包含应用于数组的真值检查的函数返回错误

algorithm - 遍历不同的唯一排列组合

Python:家庭级

python - 将 excel 嵌入到 gui 中,用于 python 程序

python - 如何在 PyQt 中将键盘焦点带到 QTextEdit 上?

python - 如何在Python扭曲框架中统计当前连接的协议(protocol)数量

arrays - 循环声音 ActionScript Flash

php - 从 php 循环创建多个 csv 文件