How time-consuming statistical functions run in Python

Source: Internet
Author: User
The examples in this paper describe how time-consuming statistical functions run in Python. Share to everyone for your reference. The implementation method is as follows:

Import Timedef Time_me (FN):  def _wrapper (*args, **kwargs):    start = Time.clock ()    fn (*args, **kwargs)    Print "%s cost%s second"% (fn.__name__, Time.clock ()-start)  return _wrapper# This adorner can be a time-consuming way to easily count function runs. #用来分析脚本的性能是最好不过了. #这样用: @time_medef Test (x, y):  time.sleep (0.1) @time_medef test2 (x):  time.sleep (0.2) test (1, 2) test2 (2) #输出: # Test cost 0.1001529524 second#test2 cost 0.199968431742 Second

Another higher-level version is:

Import Timeimport functoolsdef Time_me (info= "used"):  def _time_me (FN):    @functools. Wraps (FN)    def _ Wrapper (*args, **kwargs):      start = Time.clock ()      fn (*args, **kwargs)      print "%s%s%s"% (fn.__name__, info, Time.clock ()-start), "second"    return _wrapper  return _time_me@time_me () def test (x, y):  time.sleep ( 0.1) @time_me ("cost") def test2 (x):  time.sleep (0.2) test (1, 2) test2 (2)

Hopefully this article will help you with Python programming.

  • Related Article

    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.