How do I get time of a Python program's execution?

后端 未结 30 1635
甜味超标
甜味超标 2020-11-22 02:20

I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.

I\'ve looked at the timeit

相关标签:
30条回答
  • 2020-11-22 02:39

    The simplest way in Python:

    import time
    start_time = time.time()
    main()
    print("--- %s seconds ---" % (time.time() - start_time))
    

    This assumes that your program takes at least a tenth of second to run.

    Prints:

    --- 0.764891862869 seconds ---
    
    0 讨论(0)
  • 2020-11-22 02:41

    There is a timeit module which can be used to time the execution times of Python code.

    It has detailed documentation and examples in Python documentation, 26.6. timeit — Measure execution time of small code snippets.

    0 讨论(0)
  • 2020-11-22 02:41

    First, install humanfriendly package by opening Command Prompt (CMD) as administrator and type there - pip install humanfriendly

    Code:

    from humanfriendly import format_timespan
    import time
    begin_time = time.time()
    # Put your code here
    end_time = time.time() - begin_time
    print("Total execution time: ", format_timespan(end_time))
    

    Output:

    0 讨论(0)
  • 2020-11-22 02:42

    I put this timing.py module into my own site-packages directory, and just insert import timing at the top of my module:

    import atexit
    from time import clock
    
    def secondsToStr(t):
        return "%d:%02d:%02d.%03d" % \
            reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
                [(t*1000,),1000,60,60])
    
    line = "="*40
    def log(s, elapsed=None):
        print line
        print secondsToStr(clock()), '-', s
        if elapsed:
            print "Elapsed time:", elapsed
        print line
        print
    
    def endlog():
        end = clock()
        elapsed = end-start
        log("End Program", secondsToStr(elapsed))
    
    def now():
        return secondsToStr(clock())
    
    start = clock()
    atexit.register(endlog)
    log("Start Program")
    

    I can also call timing.log from within my program if there are significant stages within the program I want to show. But just including import timing will print the start and end times, and overall elapsed time. (Forgive my obscure secondsToStr function, it just formats a floating point number of seconds to hh:mm:ss.sss form.)

    Note: A Python 3 version of the above code can be found here or here.

    0 讨论(0)
  • 2020-11-22 02:43

    I've looked at the timeit module, but it seems it's only for small snippets of code. I want to time the whole program.

    $ python -mtimeit -n1 -r1 -t -s "from your_module import main" "main()"
    

    It runs your_module.main() function one time and print the elapsed time using time.time() function as a timer.

    To emulate /usr/bin/time in Python see Python subprocess with /usr/bin/time: how to capture timing info but ignore all other output?.

    To measure CPU time (e.g., don't include time during time.sleep()) for each function, you could use profile module (cProfile on Python 2):

    $ python3 -mprofile your_module.py
    

    You could pass -p to timeit command above if you want to use the same timer as profile module uses.

    See How can you profile a Python script?

    0 讨论(0)
  • 2020-11-22 02:47

    To use metakermit's updated answer for Python 2.7, you will require the monotonic package.

    The code would then be as follows:

    from datetime import timedelta
    from monotonic import monotonic
    
    start_time = monotonic()
    end_time = monotonic()
    print(timedelta(seconds=end_time - start_time))
    
    0 讨论(0)
提交回复
热议问题