From: forgottenwizard Date: 2007-07-30T14:10:31+09:00 Subject: Re: Performance diffrence between ifs and case On 13:47 Mon 30 Jul , Francis Cianfrocca wrote: > On 7/28/07, forgottenwizard wrote: > > > > What kind of diffrences are there, effectivly, between case and a series > > of if statements, like in this: > > > > if i="hi" > > puts "hi" > > elsif i="bye" > > puts "bye" > > else > > puts i > > end > > > > and > > > > case i > > when hi > > puts "hi" > > when bye > > puts "bye" > > else > > puts i > > end > > > > > > > > As you've written the code, the first example will probably run noticeably > faster because it will never perform a comparison. The assignment in the > first if statement will always be true, and so the first branch will always > be taken. > > To answer a related question (but one that you didn't ask): case/when uses > the === operator rather than ==, which is typically used in if/when > constructions that (unlike yours) are intended to perform comparisons rather > than assignments. > > And perhaps surprisingly, === is extremely slow when applied to arguments > that are Symbols. In those cases, I always use if/else. Actually, I'm going by the contents of ARGV[] with the actual program. I ran across symbols just awhile ago looking through a few refrences, but havn't messed with them any currently. A more-or-less realistic example would be (just using case here): case ARGV[9] when "hi", "H" puts "hi" when "bye, "B" puts "bye" else puts ARGV[9] end Don't know if that makes a diff, but all this is pretty much passed down through a single starting function as arguments to the function.