Skip to content

Instantly share code, notes, and snippets.

Created November 25, 2010 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/714786 to your computer and use it in GitHub Desktop.
Save anonymous/714786 to your computer and use it in GitHub Desktop.
require 'benchmark'
def benchmark(setting, &block)
data = (1..1000).map { block.call }
Benchmark.bm(15) do |x|
x.report("compact #{setting}") do
data.dup.each do |d|
[d[0],d[1]].compact.min
end
end
x.report("ternary #{setting}") do
data.dup.each do |d|
d[0] ? [d[0],d[1]].min : d[1]
end
end
end
end
benchmark('exists'){ [rand,rand] }
benchmark('nil'){ [nil,rand] }
user system total real
compact exists 0.000000 0.000000 0.000000 ( 0.002650)
ternary exists 0.000000 0.000000 0.000000 ( 0.003089)
user system total real
compact nil 0.000000 0.000000 0.000000 ( 0.002113)
ternary nil 0.000000 0.000000 0.000000 ( 0.000289)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment