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

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

    The time of a Python program's execution measure could be inconsistent depending on:

    • Same program can be evaluated using different algorithms
    • Running time varies between algorithms
    • Running time varies between implementations
    • Running time varies between computers
    • Running time is not predictable based on small inputs

    This is because the most effective way is using the "Order of Growth" and learn the Big "O" notation to do it properly.

    Anyway, you can try to evaluate the performance of any Python program in specific machine counting steps per second using this simple algorithm: adapt this to the program you want to evaluate

    import time
    
    now = time.time()
    future = now + 10
    step = 4 # Why 4 steps? Because until here already four operations executed
    while time.time() < future:
        step += 3 # Why 3 again? Because a while loop executes one comparison and one plus equal statement
    step += 4 # Why 3 more? Because one comparison starting while when time is over plus the final assignment of step + 1 and print statement
    print(str(int(step / 10)) + " steps per second")
    

提交回复
热议问题