本文目錄一覽:
用python 計時器怎麼做,
用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 dur( op=None, clock=[time.time()] ):
if op != None:
duration = time.time() – clock[0]
print ‘%s finished. Duration %.6f seconds.’ % (op, duration)
clock[0] = time.time()
# Example
if __name__ == ‘__main__’:
import array
dur() # Initialise the timing clock
opt1 = array.array(‘H’)
for i in range(1000):
for n in range(1000):
opt1.append(n)
dur(‘Array from append’)
opt2 = array.array(‘H’)
seq = range(1000)
for i in range(1000):
opt2.extend(seq)
dur(‘Array from list extend’)
opt3 = array.array(‘H’)
seq = array.array(‘H’, range(1000))
for i in range(1000):
opt3.extend(seq)
dur(‘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.
python的計時器
你可以用Twisted來實現,源代碼如下:
from twisted.internet import task
from twisted.internet import reactor
#set global variables
g = 0
def run():
global g
g = g + 1
print ‘the global value g is:%s’%g
#add function run to twisted’s looping call
l = task.LoopingCall(run)
#set interval to 5*60 seconds
l.start(5*60)
reactor.run()
要運行這段代碼你得裝twisted和zope中關於interface的定義,上google搜搜載一個裝了就可以了。
python如何實現計時?
用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 dur( op=None, clock=[time.time()] ):
if op != None:
duration = time.time() – clock[0]
print ‘%s finished. Duration %.6f seconds.’ % (op, duration)
clock[0] = time.time()
# Example
if __name__ == ‘__main__’:
import array
dur() # Initialise the timing clock
opt1 = array.array(‘H’)
for i in range(1000):
for n in range(1000):
opt1.append(n)
dur(‘Array from append’)
opt2 = array.array(‘H’)
seq = range(1000)
for i in range(1000):
opt2.extend(seq)
dur(‘Array from list extend’)
opt3 = array.array(‘H’)
seq = array.array(‘H’, range(1000))
for i in range(1000):
opt3.extend(seq)
dur(‘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.
python怎麼計時
定義在默認的計時器中,針對不同平台採用不同方式。在Windows上,time.clock()具有微秒精度,但是time.time()精度是1/60s。在Unix上,time.clock()有1/100s精度,而且time.time()精度遠遠更高。在另外的平台上,default_timer()測量的是牆上時鐘時間,不是CPU時間。這意味着同一計算機的其他進程可能影響計時
版權聲明:
def clock(func):
def clocked(*args, **kwargs):
t0 = timeit.default_timer()
result = func(*args, **kwargs)
elapsed = timeit.default_timer() – t0
name = func.__name__
arg_str = ‘, ‘.join(repr(arg) for arg in args)
print(‘[%0.8fs] %s(%s) – %r’ % (elapsed, name, arg_str, result))
return result
return clocked
@clock
def run(seconds):
time.sleep(seconds)
return time
if __name__ == ‘__main__’:
run(1)
本文為CSDN博主「FlyingPie」的原創文章,遵循CC 4.0 BY-SA版權協議,附上原文出處鏈接及聲明。
原文鏈接:
參考資料:CSDN。
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/130114.html