This article describes the Python implementation of a simple timer function. Share to everyone for your reference. Specifically as follows:
This function uses Python to implement a simple timer kinetic energy:
' Simple Timing Function. This function prints out a and the elapsed time from the previous. 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 du R () # 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) to 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 (100 0): 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.
I hope this article will help you with your Python programming.