Python 测量 n 次重复后循环中的增量时间
Python measuring delta time in cycle after n-repeating
拜托,我需要帮助 :) 我在 python 中有一个较长的周期,我想测量每 1000 次迭代的周期总时间以及最后 1000 次迭代之间的时间(我的意思是 1000 和 2000 之间的差异循环迭代)。
测量从我开始调用 delta
开始的总时间和调用 d2
的 1000 次迭代之间的时间的变量
start = time.time()
for i in range (0, iterations):
# ... logic in cycle ...
# calculating delta in each iteration
delta = time.time() - start
# Measuring time after every 1000 iterations
if (i + 1) % 1000 == 0:
# Calculating d2 time
d2 = time.time() - start - d2
print (str(i + 1) + ' items in ' + time.strftime("%H:%M:%S", time.gmtime(delta)) + ' (' + time.strftime("%H:%M:%S", time.gmtime(d2)) + ')' )
# After cycle
end = time.time() - start
print ('Total time: ' + time.strftime("%H:%M:%S", time.gmtime(end)))
结果如下:
1000 items in 00:00:07 (00:00:07)
2000 items in 00:00:13 (00:00:06)
3000 items in 00:00:20 (00:00:13)
4000 items in 00:00:27 (00:00:13)
5000 items in 00:00:34 (00:00:20)
6000 items in 00:00:41 (00:00:20)
7000 items in 00:00:48 (00:00:27)
8000 items in 00:00:55 (00:00:27)
9000 items in 00:01:01 (00:00:34)
10000 items in 00:01:08 (00:00:34)
11000 items in 00:01:15 (00:00:41)
12000 items in 00:01:22 (00:00:41)
似乎在第一个值中没问题(在第二行中 7 秒和 13 秒之间的增量没问题)但后来这个时间正在消散和混乱。看最后两行。 00:01:08 和 00:01:15 之间的差值约为 7 秒(如前两行),而不是 41 秒。还有最后一行...
哪里有问题?有什么想法吗?
我很乐意提供任何提示或帮助。非常感谢
此致
您还必须在每第 1000 个周期更新您的 d2。为了清楚起见,我将旧的增量命名为 old_delta
。
import time
iterations = 5000
start = time.time()
old_delta = 0
for i in range(0, iterations):
time.sleep(.002)
# ... logic in cycle ...
# calculating delta in each iteration
delta = time.time() - start
# Measuring time after every 1000 iterations
if (i + 1) % 1000 == 0:
# Calculating d2 time
d2 = delta - old_delta
print(str(i + 1) + ' items in ' + time.strftime("%H:%M:%S",
time.gmtime(delta)) + ' (' + time.strftime("%H:%M:%S", time.gmtime(d2))
+ ')' )
old_delta = delta
# After cycle
end = time.time() - start
print ('Total time: ' + time.strftime("%H:%M:%S", time.gmtime(end)))
拜托,我需要帮助 :) 我在 python 中有一个较长的周期,我想测量每 1000 次迭代的周期总时间以及最后 1000 次迭代之间的时间(我的意思是 1000 和 2000 之间的差异循环迭代)。
测量从我开始调用 delta
开始的总时间和调用 d2
start = time.time()
for i in range (0, iterations):
# ... logic in cycle ...
# calculating delta in each iteration
delta = time.time() - start
# Measuring time after every 1000 iterations
if (i + 1) % 1000 == 0:
# Calculating d2 time
d2 = time.time() - start - d2
print (str(i + 1) + ' items in ' + time.strftime("%H:%M:%S", time.gmtime(delta)) + ' (' + time.strftime("%H:%M:%S", time.gmtime(d2)) + ')' )
# After cycle
end = time.time() - start
print ('Total time: ' + time.strftime("%H:%M:%S", time.gmtime(end)))
结果如下:
1000 items in 00:00:07 (00:00:07)
2000 items in 00:00:13 (00:00:06)
3000 items in 00:00:20 (00:00:13)
4000 items in 00:00:27 (00:00:13)
5000 items in 00:00:34 (00:00:20)
6000 items in 00:00:41 (00:00:20)
7000 items in 00:00:48 (00:00:27)
8000 items in 00:00:55 (00:00:27)
9000 items in 00:01:01 (00:00:34)
10000 items in 00:01:08 (00:00:34)
11000 items in 00:01:15 (00:00:41)
12000 items in 00:01:22 (00:00:41)
似乎在第一个值中没问题(在第二行中 7 秒和 13 秒之间的增量没问题)但后来这个时间正在消散和混乱。看最后两行。 00:01:08 和 00:01:15 之间的差值约为 7 秒(如前两行),而不是 41 秒。还有最后一行...
哪里有问题?有什么想法吗?
我很乐意提供任何提示或帮助。非常感谢
此致
您还必须在每第 1000 个周期更新您的 d2。为了清楚起见,我将旧的增量命名为 old_delta
。
import time
iterations = 5000
start = time.time()
old_delta = 0
for i in range(0, iterations):
time.sleep(.002)
# ... logic in cycle ...
# calculating delta in each iteration
delta = time.time() - start
# Measuring time after every 1000 iterations
if (i + 1) % 1000 == 0:
# Calculating d2 time
d2 = delta - old_delta
print(str(i + 1) + ' items in ' + time.strftime("%H:%M:%S",
time.gmtime(delta)) + ' (' + time.strftime("%H:%M:%S", time.gmtime(d2))
+ ')' )
old_delta = delta
# After cycle
end = time.time() - start
print ('Total time: ' + time.strftime("%H:%M:%S", time.gmtime(end)))