使用Python,实现程序运行计时的数码管表示
发布网友
发布时间:2022-04-22 21:18
我来回答
共1个回答
热心网友
时间:2022-04-18 03:01
用python实现计时器功能,代码如下:
''' Simple Timing Function.
This function prints out a message with the elapsed time from the
previous call. It works with most Python 2.x platforms. The function
uses a simple trick to store a persistent variable (clock) without
using a global variable.
'''
import time
def r( op=None, clock=[time.time()] ):
if op != None:
ration = time.time() - clock[0]
print '%s finished. Duration %.6f seconds.' % (op, ration)
clock[0] = time.time()
# Example
if __name__ == '__main__':
import array
r() # Initialise the timing clock
opt1 = array.array('H')
for i in range(1000):
for n in range(1000):
opt1.append(n)
r('Array from append')
opt2 = array.array('H')
seq = range(1000)
for i in range(1000):
opt2.extend(seq)
r('Array from list extend')
opt3 = array.array('H')
seq = array.array('H', range(1000))
for i in range(1000):
opt3.extend(seq)
r('Array from array extend')
# Output:
# Array from append finished. Duration 0.175320 seconds.
# Array from list extend finished. Duration 0.068974 seconds.
# Array from array extend finished. Duration 0.001394 seconds.