Reputation: 3476
I am writing a Ruby 1.9.2 script for evaluating the execution times of different external command line calls.
I used the ruby Process.system method to execute the command line calls and tried to capture the executing time as follows:
start = Time.now
system("./script1", "argX")
puts "Duration: #{Time.now - start} seconds"
Now I have the problem that the duration doesn't reflect the execution time of the external process but the execution time of the "system" call.
Any idea how I can measure the execution time of the external process?
Upvotes: 13
Views: 11467
Reputation: 24841
You can use time
and parse the results
require 'open3'
command = './script1 argX'
stdout,stderr,status = Open3.capture3("time #{command}")
results = {}
stderr.split("\n").each do |line|
unless line.blank?
result_type,result = line.split("\t")
results[result_type] = result
end
end
puts "Clock time was #{results['real']}"
time
outputs a format like
real 0m0.003s
user 0m0.001s
sys 0m0.002s
And it outputs it to the the standard error, which is why we need to use Open3
to get it (and differentiate it from the output of your script, which hopefully will not clash with time
's output).
Note that time has a "formatted" output for the time elapsed, so you may need to do some parsing to get this into a raw milliseconds format.
Upvotes: 5
Reputation: 1808
Okay. If I understand what you are trying to do, you want to time how long the "./script1" call takes to run?
One thing you might want to do is to use the benchmark library (it's standard).
require 'benchmark'
Benchmark.bm (7) do |x|
x.report ("script1:") {system("./script1", "argX")}
end
That will generate a report with user and system times, which may be what you want.
Upvotes: 13
Reputation: 1090
If I understand correctly, you want to time the Ruby-process executing your script. When you're working on a *nix system you can use the time
utility. You can then do the following: time ruby *yourscript*
Upvotes: 2