我的代码需要使用整数UNIX时间,但是time.time()
不足以满足我的用例。它返回一个浮点数,并使用math.floor()将其强制转换为int或floor时,使用timeit.timeit进行记录会花费太长时间。
timeit.timeit(lambda : time())
的输出大约是0.12秒,对于我的用例来说足够好了,但不是整数。timeit.timeit(lambda : int(time()))
的输出约为.25秒,对于我的用例来说太长了。timeit.timeit(lambda : math.floor(time()))
约为0.36秒,是三者中最差的。
计时非常重要,所以我希望一些内置函数以int而不是float的形式返回UNIX时间。
这是我的代码段:
def main():
# The value of proceed goes up to 120.
proceed = 1
# timeout is some timestamp set at runtime, 2 days from the start of execution.
while int(time()) <= timeout:
# tps() uses int(time()), and this is what I want fixed.
if proceed == tps():
batchSend(tps())
proceed += 1
# These functions also use int(time())
tocollect = mine()
collect(tocollect)
事实证明我没有做作业。 timeit.timeit()的默认number
值设置为100万,因此实际值比我想象的要小得多。