1. 如何用python写一个计时器
可以用time.sleep(1)睡眠一秒加一,然后大于180秒是停止,用while会方便一点,
望采纳
2. 用python 计时器怎么做,
用python实现计时器功能,代码如下:
'''SimpleTimingFunction.
previouscall.ItworkswithmostPython2.xplatforms.Thefunction
(clock)without
usingaglobalvariable.
'''
importtime
defr(op=None,clock=[time.time()]):
ifop!=None:
ration=time.time()-clock[0]
print'%sfinished.Duration%.6fseconds.'%(op,ration)
clock[0]=time.time()
#Example
if__name__=='__main__':
importarray
r()#Initialisethetimingclock
opt1=array.array('H')
foriinrange(1000):
forninrange(1000):
opt1.append(n)
r('Arrayfromappend')
opt2=array.array('H')
seq=range(1000)
foriinrange(1000):
opt2.extend(seq)
r('Arrayfromlistextend')
opt3=array.array('H')
seq=array.array('H',range(1000))
foriinrange(1000):
opt3.extend(seq)
r('Arrayfromarrayextend')
#Output:
#Arrayfromappendfinished.Duration0.175320seconds.
#Arrayfromlistextendfinished.Duration0.068974seconds.
#Arrayfromarrayextendfinished.Duration0.001394seconds.
3. python怎么写计时器用面向对象和多向进程
timeit
通常在一段程序的前后都用上time.time(),然后进行相减就可以得到一段程序的运行时间,不过python提供了更强大的计时库:timeit
测试一个函数的执行时间:
此程序测试函数运行1000次的执行时间
repeat:
由于电脑永远都有其他程序也在占用着资源,你的程序不可能最高效的执行。所以一般都会进行多次试验,取最少的执行时间为真正的执行时间。
4. 关于Python里计时器的问题
HIWORD =''
def hi():
a = raw_input ("")
if a=="hi" :
HIWORD=True
hi()
while HIWORD==True:
print "hi"
5. python计时器问题
import time
print('按下回车开始计时,按下 Ctrl + C 暂停/停止计时。')
while True:
input("")
starttime = time.time()
print('开始')
pausetime=0
while True:
try:
totaltime=int((time.time()-pausetime-starttime))
print('【'+str(int((time.time()-pausetime-starttime)*0.66)),'进度】',int((time.time()-pausetime-starttime)/60),'分', int((time.time()-pausetime-starttime)%60), '秒',end='
')
time.sleep(1)
except KeyboardInterrupt:
print('【' + str(int((time.time() -pausetime- starttime) * 0.66)), '进度】', int((time.time()-pausetime - starttime) / 60), '分',
int((time.time() -pausetime- starttime) % 60), '秒', '...暂停中
按回车继续,输入Q停止计时',end='
') pausetime_start=time.time()
s=input("")
if s.strip().upper()=='Q':
pausetime_end = time.time()
pausetime = pausetime_end - pausetime_start + pausetime
print('结束')
endtime = time.time()
print('总时间:', round(endtime - pausetime - starttime, 2), '秒 ')
exit(0)
elif s.strip().upper()=='':
pausetime_end=time.time()
pausetime=pausetime_end-pausetime_start+pausetime
6. 关于python计时器的问题
当range函数里面的参数为单个变量时,这个变量#代表是长度
为6就是要从0开始数到某个元素为止,这些元素的个数一共是6个,如果按照c语言的话可能是这么写的for(i=0
;i<6;i++)。写range(6)和range(0,5)没有区别。
7. 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搜搜载一个装了就可以了。
8. Python2.7.13怎么编计时器
用python实现计时器功能,代码如下:
''SimpleTimingFunction.
previouscall.ItworkswithmostPython2.xplatforms.Thefunction
(clock)without
usingaglobalvariable.
'''
importtime
defr(op=None,clock=[time.time()]):
ifop!=None:
ration=time.time()-clock[0]
print'%sfinished.Duration%.6fseconds.'%(op,ration)
clock[0]=time.time()
#Example
if__name__=='__main__':
importarray
r()#Initialisethetimingclock
opt1=array.array('H')
foriinrange(1000):
forninrange(1000):
opt1.append(n)
r('Arrayfromappend')
opt2=array.array('H')
seq=range(1000)
foriinrange(1000):
opt2.extend(seq)
r('Arrayfromlistextend')
opt3=array.array('H')
seq=array.array('H',range(1000))
foriinrange(1000):
opt3.extend(seq)
r('Arrayfromarrayextend')
#Output:
#Arrayfromappendfinished.Duration0.175320seconds.
#Arrayfromlistextendfinished.Duration0.068974seconds.
#Arrayfromarrayextendfinished.Duration0.001394seconds.
题主空闲的时候可以多看看Python的相关教程,黑马程序员再往上有许多免费的教程,想学习的可以下载下来多看看,多学习学习,以后类似的问题就可以迎刃而解了。https://..com/question/1051689931722045579.html?fr=android_app&share_time=1499421328373
9. 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 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.
10. python关于计时器的问题
当range函数里面的参数为单个变量时,这个变量#代表是长度 为6就是要从0开始数到某个元素为止,这些元素的个数一共是6个,如果按照c语言的话可能是这么写的for(i=0 ;i<6;i++)。写range(6)和range(0,5)没有区别。