[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Performance diffrence between ifs and case

forgottenwizard

7/29/2007 1:15:00 AM

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


3 Answers

Gregory Brown

7/29/2007 3:44:00 AM

0

On 7/28/07, forgottenwizard <phrexianreaper@hushmail.com> wrote:
> What kind of diffrences are there, effectivly, between case and a series
> of if statements, like in this:

Because of the way case works, it will depend on what you are trying to do.
Use the benchmark standard library for your use case to find out which
is more efficient.

forgottenwizard

7/30/2007 5:11:00 AM

0

On 13:47 Mon 30 Jul , Francis Cianfrocca wrote:
> On 7/28/07, forgottenwizard <phrexianreaper@hushmail.com> 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.


Harold Burton

12/10/2010 3:15:00 AM

0

In article <4d0199cf$0$1605$742ec2ed@news.sonic.net>,
rfischer@sonic.net (Ray Fischer) wrote:

> NefeshBarYochai <tachnan@hotmail.com> wrote:
> >On Dec 7, 10:49?pm, rfisc...@sonic.net (Ray Fischer) wrote:
> >> dsharavi ?<dshara...@hotmail.com> wrote:
> >> >On Dec 7, 10:56 am, rfisc...@sonic.net (Ray Fischer) wrote:
> >> >> dsharavi <dshara...@hotmail.com> wrote:
> >> >> >On Dec 6, 1:26 am, rfisc...@sonic.net (Ray Fischer) wrote:
> >> >> >> Harold Burton <hal.i.bur...@hotmail.com> wrote:
> >>
> >> >> >> > Benj <bjac...@iwaynet.net> wrote:
> >>
> >> >> >> >> On Dec 5, 12:16 am, "The Peeler" <foreskinpee...@alltimes.com>
> >> >> >> >> wrote:
> >>
> >> >> >> >> > No it is not twaddle. "Jew only" roads are common in Israel and
> >> >> >> >> > the Middle
> >> >> >> >> > East.
> >>
> >> >> >> >> Which means that . . .
> >>
> >> >> >> > . . . you're stupid enough to believe nonsense. Please provide
> >> >> >> >evidence of "Jew only" roads. Nutcase websites don't count.
> >>
> >> >> >> Technically they're "Israeli only" roads, and since Israel is
> >> >> >> predominantly Jewish, the roads are de facto Jewish only. They
> >> >> >> certainly are not for the use of Palestinians, even though the roads
> >> >> >> are in the West Bank.
> >>
> >> >> >There is only one "Israelis only" road -- NOT "Jews only" road,
> >> >> >"ISRAELIS only" road -- and that is Route 60 from Jerusalem to
> >> >> >Hebron.
> >>
> >> >> Since ISrael is a Jewish state and since non-Jewish Israelis are a
> >> >> small minority, it is effectively a Jewish-only road.
> >>
> >> >> > Similarly, there are "Palestinians only" roads on which
> >>
> >> >> The "Palestinian only" roads are not in Israel.
> >>
> >> >That's a remarkably stupid comment.
> >>
> >> I understand that you neonazi bigots cannot cope with the truth of
> >> your evil, but your fatherland really is a brutal dictatorship.
> >
> >Ray you've been touching your penis to long.
>
> And there is a fine example of the sanity of the fascist anti-Arab
> bigot. It's easy to justify brutal killings when you're insane.

and that means you haven't been touching your penis too long, why?


snicker