Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On platforms where it's available, <a href="http://docs.python.org/2/library/resource.html" rel="nofollow">the <code>resource</code> module</a> may provide what you need. If you need to time multiple commands simultaneously, you may want to (for each command you want to run) <a href="http://docs.python.org/2/library/os.html#os.fork" rel="nofollow">fork</a> and then create the subprocess so you get information for only that process. Here's one way you might do this:</p> <pre><code>def start_running(command): time_read_pipe, time_write_pipe = os.pipe() want_read_pipe, want_write_pipe = os.pipe() runner_pid = os.fork() if runner_pid != 0: os.close(time_write_pipe) os.close(want_read_pipe) def finish_running(): os.write(want_write_pipe, 'x') os.close(want_write_pipe) time = os.read(time_read_pipe, struct.calcsize('f')) os.close(time_read_pipe) time = struct.unpack('f', time)[0] return time return finish_running os.close(time_read_pipe) os.close(want_write_pipe) sub_pid = os.fork() if sub_pid == 0: os.close(time_write_pipe) os.close(want_read_pipe) os.execvp(command[0], command) os.wait() usage = resource.getrusage(resource.RUSAGE_CHILDREN) os.read(want_read_pipe, 1) os.write(time_write_pipe, struct.pack('f', usage.ru_utime)) sys.exit(0) </code></pre> <p>You can then use it to run a few commands:</p> <pre><code>get_ls_time = start_running(['ls']) get_work_time = start_running(['python', '-c', 'print (2 ** 512) ** 200']) </code></pre> <p>After that code has executed, both of those commands should be running in parallel. When you want to wait for them to finish and get the time they took to execute, call the function returned by <code>start_running</code>:</p> <pre><code>ls_time = get_ls_time() work_time = get_work_time() </code></pre> <p>Now <code>ls_time</code> will contain the time <code>ls</code> took to execute and <code>work_time</code> will contain the time <code>python -c "print (2 ** 512) ** 200"</code> took to execute.</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. 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.
    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