John said:
a) Phlip emits very valid and important nostrums.
b) Definitely not a moron, you'll miss some Good Stuff if you make that
mistake.
c) Once anyone actually benchmarks the difference and hence contributes
real data to the
discussion....
logic_benchmark.rb:
require 'benchmark'
def nop
end
def if_equiv_method(str)
if "hi" === str
nop
elsif "bye" === str
nop
else
nop
end
end
def if_method(str)
if str == "hi"
nop
elsif str == "bye"
nop
else
nop
end
end
def case_method(str)
case str
when "hi"
nop
when "bye"
nop
else
nop
end
end
strings = %w{hi bye foo}
Benchmark.bmbm(20) do |x|
x.report("if"){for i in 0..1000000 do
if_method(strings[rand(strings.length)]) end }
x.report("if_equiv"){for i in 0..1000000 do
if_equiv_method(strings[rand(strings.length)]) end }
x.report("case"){for i in 0..1000000 do
case_method(strings[rand(strings.length)]) end }
end
Results:
C:\WINDOWS\system32\cmd.exe /c ruby logic_benchmark.rb
Rehearsal -------------------------------------------------------
if 2.578000 0.000000 2.578000 ( 2.610000)
if_equiv 2.797000 0.000000 2.797000 ( 2.953000)
case 2.625000 0.000000 2.625000 ( 2.812000)
---------------------------------------------- total: 8.000000sec
user system total real
if 2.578000 0.000000 2.578000 ( 2.625000)
if_equiv 2.781000 0.000000 2.781000 ( 2.828000)
case 2.625000 0.000000 2.625000 ( 2.672000)
It's pretty marginal, and I did have them come out much closer on some runs.
Write it up here...
http://rubygarden.org/ruby/page/show/RubyOptimization
...the wiki page where I keep breadcrumbs of info for when desperation
requires me to shovel perfectly correct nostrums to one side and put in
a twist of
speed.
Speaking of which... Rubygarden seems to be slow..
Too slow for me to write this up, as a matter of fact.