[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Performance diffrence between ifs and case

John Carter

7/29/2007 9:58:00 PM

9 Answers

M. Edward (Ed) Borasky

7/29/2007 10:56:00 PM

0

John Carter wrote:
> Speaking of which... Rubygarden seems to be slow..
>
> Gah! Spammers have totally vandalized that page. Twice. Hmm. That's
> probably
> why it's so slow, probably a major spam attack...
>
> I hate spammers. Really the complete scum of the earth. The Lambda the
> Ultimate (excellent programming language design site)
> http://lambda-the-ul... crew have also been under attack.

Nobody is willing to do what it will take to eliminate spam (and for
that matter, junk snail mail), namely, tax it. :)


forgottenwizard

7/30/2007 3:37:00 AM

0

If you can come up with a way to tax spammers w/o causing problems for
normal traffic and fully eliminating spam, then by all means do so.
Personally, I'd prefer just find who is responsibe, and beat them to
within an inch of their lives and remove all their computer rights
permanently.

On 07:55 Mon 30 Jul , M. Edward (Ed) Borasky wrote:
> John Carter wrote:
> > Speaking of which... Rubygarden seems to be slow..
> >
> > Gah! Spammers have totally vandalized that page. Twice. Hmm. That's
> > probably
> > why it's so slow, probably a major spam attack...
> >
> > I hate spammers. Really the complete scum of the earth. The Lambda the
> > Ultimate (excellent programming language design site)
> > http://lambda-the-ul... crew have also been under attack.
>
> Nobody is willing to do what it will take to eliminate spam (and for
> that matter, junk snail mail), namely, tax it. :)
>
>


John Carter

7/30/2007 4:03:00 AM

0

Alex Young

7/30/2007 8:01:00 AM

0

John Carter wrote:
> On Sun, 29 Jul 2007, Sean Surname wrote:
>
>> Phlip wrote:
>>> forgottenwizard wrote:
>>>
>>>> What kind of diffrences are there, effectivly, between case and a
>>>> series
>>>> of if statements, like in this:
>>>
>>> Google for "premature optimization is the root of all evil".
>>
>> Classify as "nostrum-emitting moron."
>
> 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/RubyOp...
> ...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.

--
Alex


Robert Dober

7/30/2007 11:15:00 AM

0

On 7/30/07, John Carter <john.carter@tait.co.nz> wrote:
> On Mon, 30 Jul 2007, forgottenwizard wrote:
> > Personally, I'd prefer just find who is responsibe, and beat them to
> > within an inch of their lives and remove all their computer rights
> > permanently.
>
> And once we've got rid of them...
>
> ..we can go after those who persist in extending tangential and off
> topic threads... ;-D
No, your logic is important as of course everybody of us will be the
single sole survivor on earth :(
>
> Sorry, couldn't resist a good sharp leg pull.
Which was well deserved.
Robert

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

Gregory Brown

7/30/2007 2:23:00 PM

0

On 7/30/07, Alex Young <alex@blackkettle.org> wrote:

> Benchmark.bmbm(20) do |x|
> x.report("if"){for i in 0..1000000 do

Yuck!

1000000.times do ... end

Alex Young

7/30/2007 2:35:00 PM

0

Gregory Brown wrote:
> On 7/30/07, Alex Young <alex@blackkettle.org> wrote:
>
>> Benchmark.bmbm(20) do |x|
>> x.report("if"){for i in 0..1000000 do
>
> Yuck!
>
> 1000000.times do ... end
>
Meh.

require 'benchmark'

def nop
end

Benchmark.bmbm(20) do |x|
x.report("range"){for i in 0..1000000 do nop end}
x.report("times"){1000000.times do nop end}
end

Rehearsal -------------------------------------------------------
range 0.578000 0.000000 0.578000 ( 0.594000)
times 0.531000 0.000000 0.531000 ( 0.578000)
---------------------------------------------- total: 1.109000sec

user system total real
range 0.578000 0.000000 0.578000 ( 0.594000)
times 0.578000 0.000000 0.578000 ( 0.578000)

--
Alex

James Gray

7/30/2007 2:42:00 PM

0

On Jul 30, 2007, at 9:35 AM, Alex Young wrote:

> Gregory Brown wrote:
>> On 7/30/07, Alex Young <alex@blackkettle.org> wrote:
>>> Benchmark.bmbm(20) do |x|
>>> x.report("if"){for i in 0..1000000 do
>> Yuck!
>> 1000000.times do ... end
> Meh.

It has nothing to do with timing. It's about ugly verses pretty. :)

James Edward Gray II

Alex Young

7/30/2007 3:15:00 PM

0

James Edward Gray II wrote:
> On Jul 30, 2007, at 9:35 AM, Alex Young wrote:
>
>> Gregory Brown wrote:
>>> On 7/30/07, Alex Young <alex@blackkettle.org> wrote:
>>>> Benchmark.bmbm(20) do |x|
>>>> x.report("if"){for i in 0..1000000 do
>>> Yuck!
>>> 1000000.times do ... end
>> Meh.
>
> It has nothing to do with timing. It's about ugly verses pretty. :)
To be perfectly honest, I had it fixed in my head that for...in... was
faster than N.times {} when I wrote the original code, and was just
trying to get rid of an unnecessary overhead. Just shows what use
assumptions like that are...

--
Alex