Python implements a simple timer function _python

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.