Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to measure time spent waiting for I/O?

Tags:

python

I've used profile on functions in my Python script to get their execution time, and I've used the Unix time command to get the real, user and sys time of the script overall, but I can't find how to measure the time spent waiting for I/O in Python.

In my script I query various database, can I measure the time it takes to send and receive information?

like image 320
AnonyMouse Avatar asked Oct 26 '25 15:10

AnonyMouse


1 Answers

you can use a Decorator like this that allows you to measure the execution times of dedicated methods :

import time                                                

def measure_time(f):

  def timed(*args, **kw):
    ts = time.time()
    result = f(*args, **kw)
    te = time.time()

    print '%r (%r, %r) %2.2f sec' % \
          (f.__name__, args, kw, te-ts)
    return result

return timed

You can use it like this :

  @measure_time
  def foo():
        #content of function 

note that f.__name__ return the name of function ! (in this case 'foo')

like image 147
Mazdak Avatar answered Oct 28 '25 04:10

Mazdak