Created
November 25, 2010 01:59
-
-
Save anonymous/714786 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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