How do I get monotonic time durations in python?

前端 未结 5 882
感情败类
感情败类 2020-11-27 03:09

I want to log how long something takes in real walltime. Currently I\'m doing this:

startTime = time.time()
someSQLOrSomething()
print \"That took %.3f secon         


        
5条回答
  •  长情又很酷
    2020-11-27 04:03

    Here's how I get monotonic time in Python 2.7:

    Install the monotonic package:

    pip install monotonic
    

    Then in Python:

    import monotonic; mtime = monotonic.time.time #now mtime() can be used in place of time.time()
    
    t0 = mtime()
    #...do something
    elapsed = mtime()-t0 #gives correct elapsed time, even if system clock changed.
    

提交回复
热议问题