Skip to content

Instantly share code, notes, and snippets.

@bf4
Last active January 1, 2016 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/8184145 to your computer and use it in GitHub Desktop.
Save bf4/8184145 to your computer and use it in GitHub Desktop.
Benchmarking system calls maybe

Benchmarking calls to /usr/bin/true on osx with ruby 1.9.3-p484 built on rvm

Calculating -------------------------------------
            backtick        56 i/100ms
              system        61 i/100ms
                open        59 i/100ms
             open |-        41 i/100ms
               open3        72 i/100ms
              thread        51 i/100ms
-------------------------------------------------
            backtick      578.8 (±1.6%) i/s -       2912 in   5.032061s
              system      620.5 (±1.8%) i/s -       3111 in   5.015363s
                open      596.2 (±1.5%) i/s -       3009 in   5.048177s
             open |-      417.2 (±2.9%) i/s -       2091 in   5.015907s
               open3      773.9 (±2.1%) i/s -       3888 in   5.026007s
              thread      469.4 (±12.1%) i/s -       2346 in   5.072437s
require 'benchmark'
require 'benchmark/ips'
require 'open3'
# require 'open4'
ENV['RUBYOPT'] = nil
$VERBOSE = nil
Benchmark.ips do |x|
# Benchmark.bmbm do |x|
x.report("backtick") do
`/usr/bin/true`
end
x.report("system") do
system("/usr/bin/true")
end
x.report("open") do
cmd = open("|/usr/bin/true")
cmd.close
end
x.report("open |-") do
open("|-") do |f|
system("/usr/bin/true")
end
end
x.report("open3") do
Open3.popen3("/usr/bin/true")
end
x.report("thread") do
Thread.new do
system("/usr/bin/true")
end.join
end
# exec
# warnings
# x.report("%x") do
# %x("/usr/bin/true")
# end
# end
# forks too many processes
# x.report("open4") do
# Open4.popen4("/usr/bin/true")
# end
# forks too many processes
# x.report("popen") do
# IO.popen("/usr/bin/true") #{}
# end
# forks too many processes
# x.report("spawn") do
# Process.spawn("/usr/bin/true")
# end
# x.report("fork") do
# Kernel.fork do
# Kernel.exec("/usr/bin/true")
# end
# end
# Trying to figure out Kernel.fork, Kernel.exec, IO.pipe, IO.select
# x.report("pipe") do
# rp, wp = IO.pipe
# mesg = "ping "
# 100.times {
# # IO.select follows IO#read. Not the best way to use IO.select.
# rs, ws, = IO.select([rp], [wp])
# if r = rs[0]
# ret = r.read(5)
# print ret
# case ret
# when /ping/
# mesg = "pong\n"
# when /pong/
# mesg = "ping "
# end
# end
# if w = ws[0]
# w.write(mesg)
# end
# }
# end
# java.lang.Runtime.get_runtime.exec("rvm use foo").get_output_stream.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment