我有这个简单的 python 代码,
import time
t1 = time.time()
total = sum([x for x in range(1000) if x%3 is 0 or x%5 is 0])
t2 =time.time()
print "Ans is: %d, Time is %f" %(total,(t2-t1)*1000.0)
我得到这个作为我的输出
Ans is: 233168, Time is 0.000000
我怎么看不到我的号码结果?我在 google 上搜索了用字符串替换数字的其他示例,它们完全符合我的要求,有什么帮助吗?
我也尝试过使用 %0.3f 和 %d...嗯...在此先感谢您的帮助
编辑
当我尝试这段代码时:
import time
t1 = time.time()
total = sum([x for x in range(1000) if x%3 is 0 or x%5 is 0])
t2 =time.time()
print t2
print "Ans is: %d, Time is %f" %(total,(t2-t1)*1000.0)
我得到了这个答案,
1344350841.39
Ans is: 233168, Time is 15.000105
请您参考如下方法:
这不是您问题的直接答案,但如果您使用 Windows,我宁愿推荐使用 clock()。根据文档,它在 UNIX/MacOS 上的行为不同。
from time import clock
clock()
total = sum([x for x in range(1000) if x%3 == 0 or x%5 == 0])
print "Ans is: %d, Time is %f" %(total,clock())
