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

后端 未结 30 1644
甜味超标
甜味超标 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:56

    I was having the same problem in many places, so I created a convenience package horology. You can install it with pip install horology and then do it in the elegant way:

    from horology import Timing
    
    with Timing(name='Important calculations: '):
        prepare()
        do_your_stuff()
        finish_sth()
    

    will output:

    Important calculations: 12.43 ms
    

    Or even simpler (if you have one function):

    from horology import timed
    
    @timed
    def main():
        ...
    

    will output:

    main: 7.12 h
    

    It takes care of units and rounding. It works with python 3.6 or newer.

提交回复
热议问题