How to measure time taken between lines of code in python?

前端 未结 7 873
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 11:00

So in Java, we can do How to measure time taken by a function to execute

But how is it done in python? To measure the time start and end time between lines of codes?

7条回答
  •  有刺的猬
    2020-12-04 12:07

    I always prefer to check time in hours, minutes and seconds (%H:%M:%S) format:

    from datetime import datetime
    start = datetime.now()
    # your code
    end = datetime.now()
    time_taken = end - start
    print('Time: ',time_taken) 
    

    output:

    Time:  0:00:00.000019
    

提交回复
热议问题