[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/30/2007 9:11:00 PM

7 Answers

Alex Young

7/30/2007 9:15:00 PM

0

John Carter wrote:
> On Mon, 30 Jul 2007, Alex Young wrote:
>
>>> 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.
>
> http://rubygarden.org/Ruby/page/show/CaseI...
>
Fab. Thanks for that - I wouldn't have got round to it.

--
Alex

Paul

7/31/2007 11:41:00 AM

0

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}
x.report("each"){(0..1000000).each do nop end}
end
------------------------------------------------
range 0.657000 0.000000 0.657000 ( 0.656000)
times 0.609000 0.000000 0.609000 ( 0.609000)
each 0.594000 0.000000 0.594000 ( 0.594000)
----------------------------- total: 1.860000sec

user system total real
range 0.641000 0.000000 0.641000 ( 0.640000)
times 0.594000 0.000000 0.594000 ( 0.594000)
each 0.578000 0.000000 0.578000 ( 0.594000)


Gregory Brown

8/1/2007 5:02:00 PM

0

On 7/31/07, Paul <Paul.McArdles@gmail.com> wrote:
> 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}
> x.report("each"){(0..1000000).each do nop end}
> end
> ------------------------------------------------
> range 0.657000 0.000000 0.657000 ( 0.656000)
> times 0.609000 0.000000 0.609000 ( 0.609000)
> each 0.594000 0.000000 0.594000 ( 0.594000)
> ----------------------------- total: 1.860000sec
>
> user system total real
> range 0.641000 0.000000 0.641000 ( 0.640000)
> times 0.594000 0.000000 0.594000 ( 0.594000)
> each 0.578000 0.000000 0.578000 ( 0.594000)

I'll remember this next time I want to save 0.03 s per 1000000 iterations.

<groans>

My original point was that if you want to do something n times, use n.times
:)

Alex Young

8/1/2007 6:13:00 PM

0

Gregory Brown wrote:
> On 7/31/07, Paul <Paul.McArdles@gmail.com> wrote:
>> 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}
>> x.report("each"){(0..1000000).each do nop end}
>> end
>> ------------------------------------------------
>> range 0.657000 0.000000 0.657000 ( 0.656000)
>> times 0.609000 0.000000 0.609000 ( 0.609000)
>> each 0.594000 0.000000 0.594000 ( 0.594000)
>> ----------------------------- total: 1.860000sec
>>
>> user system total real
>> range 0.641000 0.000000 0.641000 ( 0.640000)
>> times 0.594000 0.000000 0.594000 ( 0.594000)
>> each 0.578000 0.000000 0.578000 ( 0.594000)
>
> I'll remember this next time I want to save 0.03 s per 1000000 iterations.
>
> <groans>
>
> My original point was that if you want to do something n times, use n.times
> :)
>
My point was going to be that the one place optimisation really matters
is in benchmark code - you only want to measure the effect you're
looking for - but I managed to shoot myself in the foot and prove
another point. Don't assume, measure! :-)

--
Alex

Gregory Brown

8/1/2007 6:32:00 PM

0

On 8/1/07, Alex Young <alex@blackkettle.org> wrote:

> My point was going to be that the one place optimisation really matters
> is in benchmark code - you only want to measure the effect you're
> looking for - but I managed to shoot myself in the foot and prove
> another point. Don't assume, measure! :-)

If that's the case you shouldn't be making a nop() method call.
That's a *huge* part of what you're seeing there.

David A. Black

8/1/2007 6:45:00 PM

0

Alex Young

8/1/2007 7:20:00 PM

0

dblack@rubypal.com wrote:
> Hi --
>
> On Thu, 2 Aug 2007, Alex Young wrote:
>
>> Gregory Brown wrote:
>>> On 7/31/07, Paul <Paul.McArdles@gmail.com> wrote:
>>>> 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}
>>>> x.report("each"){(0..1000000).each do nop end}
>>>> end
>>>> ------------------------------------------------
>>>> range 0.657000 0.000000 0.657000 ( 0.656000)
>>>> times 0.609000 0.000000 0.609000 ( 0.609000)
>>>> each 0.594000 0.000000 0.594000 ( 0.594000)
>>>> ----------------------------- total: 1.860000sec
>>>>
>>>> user system total real
>>>> range 0.641000 0.000000 0.641000 ( 0.640000)
>>>> times 0.594000 0.000000 0.594000 ( 0.594000)
>>>> each 0.578000 0.000000 0.578000 ( 0.594000)
>>>
>>> I'll remember this next time I want to save 0.03 s per 1000000
>>> iterations.
>>>
>>> <groans>
>>>
>>> My original point was that if you want to do something n times, use
>>> n.times
>>> :)
>>>
>> My point was going to be that the one place optimisation really
>> matters is in benchmark code - you only want to measure the effect
>> you're looking for - but I managed to shoot myself in the foot and
>> prove another point. Don't assume, measure! :-)
>
> Even if one of the iterators (each or times) was much faster than the
> other, as long as you use the same one every time you're controlling
> for that. You just wouldn't want to go back and forth.
'Tis true, 'tis true. It was not my finest hour :-)

--
Alex