[Python] python实现的简单的计时器功能函数 →→→→→进入此内容的聊天室

来自 , 2019-04-11, 写在 Python, 查看 122 次.
URL http://www.code666.cn/view/7b647a7d
  1. ''' Simple Timing Function.
  2. This function prints out a message with the elapsed time from the
  3. previous call. It works with most Python 2.x platforms. The function
  4. uses a simple trick to store a persistent variable (clock) without
  5. using a global variable.
  6. '''
  7. import time
  8. def dur( op=None, clock=[time.time()] ):
  9.     if op != None:
  10.         duration = time.time() - clock[0]
  11.         print '%s finished. Duration %.6f seconds.' % (op, duration)
  12.     clock[0] = time.time()
  13.  
  14. # Example
  15. if __name__ == '__main__':
  16.     import array
  17.     dur()   # Initialise the timing clock
  18.    
  19.     opt1 = array.array('H')
  20.     for i in range(1000):
  21.         for n in range(1000):
  22.             opt1.append(n)
  23.     dur('Array from append')
  24.    
  25.     opt2 = array.array('H')
  26.     seq = range(1000)
  27.     for i in range(1000):
  28.         opt2.extend(seq)
  29.     dur('Array from list extend')
  30.    
  31.     opt3 = array.array('H')
  32.     seq = array.array('H', range(1000))
  33.     for i in range(1000):
  34.         opt3.extend(seq)
  35.     dur('Array from array extend')
  36.  
  37. # Output:
  38. # Array from append finished. Duration 0.175320 seconds.
  39. # Array from list extend finished. Duration 0.068974 seconds.
  40. # Array from array extend finished. Duration 0.001394 seconds.
  41.  
  42. #//python/8910

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

这儿你可以回复上面这条便签

captcha