Performance diffrence between ifs and case

A

Alex Young

James said:
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...
 
P

Paul

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)
 
G

Gregory Brown

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
:)
 
A

Alex Young

Gregory said:
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! :)
 
K

Kaldrenon

Less to type in

case i
when arg,a
...
when arb,b
..

instead of
if i="arg" or i="a"

:)

Just a thought from an iffer (cases are ugly in Java, so I just got
used to always iffing):

case i
when arg,a
...
when arb,b
...
end

isn't much different from

if [arg,a].include?( i )
...
elsif [arb,b].include?( i )
...
end

include? is possibly less efficient, though. Haven't benched it.
 
G

Gregory Brown

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.
 
D

dblack

Hi --

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.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
A

Alex Young

Hi --



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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top