您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python实现简单的计时器功能函数

2026/1/31 0:35:52发布18次查看
本文实例讲述了python实现简单的计时器功能函数。分享给大家供大家参考。具体如下:
此函数通过python实现了一个简单的计时器动能:
''' simple timing function.this function prints out a message with the elapsed time from theprevious call. it works with most python 2.x platforms. the functionuses a simple trick to store a persistent variable (clock) withoutusing a global variable.'''import timedef 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()# exampleif __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程序设计有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product