From: Francis Cianfrocca Date: 2007-07-30T13:47:27+09:00 Subject: Re: Performance diffrence between ifs and case ------=_Part_30567_4071104.1185770850466 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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. ------=_Part_30567_4071104.1185770850466--