用python 计时器怎么做,
用python实现计时器功能,代码如下:
成都创新互联公司主营云冈网站建设的网络公司,主营网站建设方案,APP应用开发,云冈h5成都小程序开发搭建,云冈网站营销推广欢迎云冈等地区企业咨询
''' 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如何实现计时?
用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.
Python2.7.13怎么编计时器
用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的相关教程,黑马程序员再往上有许多免费的教程,想学习的可以下载下来多看看,多学习学习,以后类似的问题就可以迎刃而解了。;share_time=1499421328373
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='\r')
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), '秒', '...暂停中\r\n按回车继续,输入Q停止计时',end='\r') 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
python怎么写计时器用面向对象和多向进程
timeit
通常在一段程序的前后都用上time.time(),然后进行相减就可以得到一段程序的运行时间,不过python提供了更强大的计时库:timeit
#导入timeit.timeitfrom timeit import timeit
#看执行1000000次x=1的时间:timeit('x=1')#看x=1的执行时间,执行1次(number可以省略,默认值为1000000):timeit('x=1', number=1)#看一个列表生成器的执行时间,执行1次:timeit('[i for i in range(10000)]', number=1)#看一个列表生成器的执行时间,执行10000次:timeit('[i for i in range(100) if i%2==0]', number=10000)
测试一个函数的执行时间:
from timeit import timeitdef func():
s = 0
for i in range(1000):
s += i print(s)# timeit(函数名_字符串,运行环境_字符串,number=运行次数)t = timeit('func()', 'from __main__ import func', number=1000)print(t)
此程序测试函数运行1000次的执行时间
repeat:
由于电脑永远都有其他程序也在占用着资源,你的程序不可能最高效的执行。所以一般都会进行多次试验,取最少的执行时间为真正的执行时间。
from timeit import repeatdef func():
s = 0
for i in range(1000):
s += i#repeat和timeit用法相似,多了一个repeat参数,表示重复测试的次数(可以不写,默认值为3.),返回值为一个时间的列表。t = repeat('func()', 'from __main__ import func', number=100, repeat=5)print(t)
print(min(t))
文章标题:python函数计时器的简单介绍
文章出自:http://scgulin.cn/article/doccgos.html