Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>On UNIX:</strong> either (a) use <a href="http://docs.python.org/2/library/resource.html">resource</a> module (also see answer by icktoofay), or (b) use the <a href="http://linux.die.net/man/7/time">time</a> command and parse the results, or (c) use <a href="http://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html">/proc</a> filesystem, parse /proc/[pid]/stat and parse out <code>utime</code> and <code>stime</code> fields. The last of these is Linux-specific.</p> <p>Example of using <code>resource</code>:</p> <pre><code>import subprocess, resource usage_start = resource.getrusage(resource.RUSAGE_CHILDREN) subprocess.call(["yourcommand"]) usage_end = resource.getrusage(resource.RUSAGE_CHILDREN) cpu_time = usage_end.ru_utime - usage_start.ru_utime </code></pre> <p>Note: it is not necessary to do fork/execvp, <code>subprocess.call()</code> or the other <a href="http://docs.python.org/2/library/subprocess.html">subprocess</a> methods are fine here and much easier to use.</p> <p>Note: you could run multiple commands from the same python script simultaneously either using subprocess.Popen or subprocess.call and threads, but resource won't return their correct individual cpu times, it will return the sum of their times in between calls to getrusage; to get the individual times, run one little python wrapper per command to time it as above (could launch those from your main script), or use the <code>time</code> method below which <em>will</em> work correctly with multiple simultaneous commands (time is basically just such a wrapper).</p> <p>Example of using <code>time</code>:</p> <pre><code>import subprocess, StringIO time_output = StringIO.StringIO() subprocess.call(["time", "yourcommand", "youroptions"], stdout=time_output) # parse time_output </code></pre> <p><strong>On Windows:</strong> You need to use performance counters (aka "performance data helpers") somehow. Here is a <a href="http://en.literateprograms.org/CPU_usage_using_performance_counters_%28C,_Windows_2000%29">C example</a> of the underlying API. To get it from python, you can use one of two modules: <a href="http://docs.activestate.com/activepython/2.7/pywin32/win32pdh.html">win32pdh</a> (part of pywin32; <a href="http://www.cac.cornell.edu/wiki/index.php?title=Performance_Data_Helper_in_Python_with_win32pdh">sample code</a>) or <a href="http://code.google.com/p/pyrfcon/">pyrfcon</a> (cross-platform, also works on Unix; <a href="http://code.google.com/p/pyrfcon/source/browse/trunk/example.py">sample code</a>).</p> <p>Any of these methods actually meet the "hard mode" requirements above: they should be accurate even with multiple running instances of different processes on a busy system. They may not produce the exact same results in that case compared to running just one process on an idle system, because process switching does have some overhead, but they will be very close, because they ultimately get their data from the OS scheduler.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload