Skip to content

Instantly share code, notes, and snippets.

@284km
Created February 21, 2018 05:24
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 284km/35514b7dcf8128777c5656cc3b4b388e to your computer and use it in GitHub Desktop.
Save 284km/35514b7dcf8128777c5656cc3b4b388e to your computer and use it in GitHub Desktop.
分かってはいたがまあやってみて、same-ish だった。
require 'benchmark/ips'
def ternary_equal
(true == true) ? true : false
end
def ternary_not_equal
(true != false) ? true : false
end
def if_equal
if(true == true)
true
else
false
end
end
def if_not_equal
if(true != false)
true
else
false
end
end
Benchmark.ips do |x|
x.report('ternary_equal') { ternary_equal }
x.report('ternary_not_equal') { ternary_not_equal }
x.report('if_equal') { if_equal }
x.report('if_not_equal') { if_not_equal }
x.compare!
end
__END__
$ ruby ternary_equal_notequal.rb
Warming up --------------------------------------
ternary_equal 192.244k i/100ms
ternary_not_equal 133.545k i/100ms
if_equal 125.349k i/100ms
if_not_equal 151.554k i/100ms
Calculating -------------------------------------
ternary_equal 5.941M (±21.3%) i/s - 28.260M in 5.028214s
ternary_not_equal 4.658M (±20.0%) i/s - 22.168M in 5.024899s
if_equal 5.467M (±20.5%) i/s - 25.822M in 5.028798s
if_not_equal 3.616M (±26.6%) i/s - 16.065M in 5.009075s
Comparison:
ternary_equal: 5941095.7 i/s
if_equal: 5467124.3 i/s - same-ish: difference falls within error
ternary_not_equal: 4657866.1 i/s - same-ish: difference falls within error
if_not_equal: 3615514.5 i/s - 1.64x slower
ruby tmp2.rb 24.16s user 0.25s system 84% cpu 28.943 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment