Skip to content

Instantly share code, notes, and snippets.

@adrienthebo
Created January 22, 2014 06:42
Show Gist options
  • Save adrienthebo/8554389 to your computer and use it in GitHub Desktop.
Save adrienthebo/8554389 to your computer and use it in GitHub Desktop.
ruby command execution
#!/usr/bin/env ruby
require 'benchmark'
require 'childprocess'
require 'systemu'
argv = %w[git rev-parse HEAD^{commit}]
args = argv.join(" ")
COUNT = 100
Benchmark.bmbm do |b|
b.report("%x") do
COUNT.times { _ = %x(#{args}) }
end
b.report("system") do
COUNT.times { _ = system("#{args} >/dev/null") }
end
b.report("childprocess") do
COUNT.times do
process = ChildProcess.build(*argv)
stdout_r, stdout_w = IO.pipe
stderr_r, stderr_w = IO.pipe
process.duplex = true
process.io.stdout = stdout_w
process.io.stderr = stderr_w
process.cwd = Dir.getwd
process.start
process.wait
stdout_w.close
stderr_w.close
stdout = ""
stdout << stdout_r.read_nonblock(1 << 12) until stdout_r.eof?
end
end
b.report("systemu") do
COUNT.times { status, out, err = systemu(args) }
end
end
Executing "git rev-parse HEAD^{commit}" 100 times
Rehearsal ------------------------------------------------
%x 0.010000 0.040000 0.260000 ( 0.325595)
system 0.000000 0.030000 0.160000 ( 0.295756)
childprocess 0.000000 0.040000 0.140000 ( 0.173006)
systemu 0.080000 0.050000 19.800000 ( 19.873185)
-------------------------------------- total: 20.360000sec
user system total real
%x 0.010000 0.040000 0.250000 ( 0.295944)
system 0.010000 0.030000 0.180000 ( 0.301151)
childprocess 0.010000 0.040000 0.150000 ( 0.175649)
systemu 0.060000 0.070000 19.740000 ( 19.797458)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment