Terence Chow
Terence Chow

Reputation: 11173

Which is faster, separate conditionals, or combined with elsif?

In ruby, is there any difference between pseudocode like:

if n > 2:
   do something to A
end

if n > 4:
   do something to B
end

if n >25:
   do something to C
end

vs

if n>2 && n <4:
  do something to A
elif n >4 && n < 25:
  do something to A and do something to B
elif n > 25:
  do something to A and B and C
end

As you can see they do the same thing, but one is easier to read and write if you have a lot of the if then statements. However if the multiple if blocks are taking up more processing power then it would not be worth it to write it that way.

Therefore I'm wondering if there is a difference between the two? Conceptual answer is fine, I don't need to benchmark anything

Upvotes: 1

Views: 110

Answers (4)

sawa
sawa

Reputation: 168269

No there is no difference. Both programs will return a syntax error, and will not run.

Upvotes: 1

nbarraille
nbarraille

Reputation: 10023

1/ they are not equivalent.

The first statement is equivalent to

if n>2 && n <= 4:
  do something to A
elif n > 4 && n <= 25:
  do something to A
  do something to B
elif n > 25:
  do something to A
  do something to B
  do something to C
end

If do something to A returns nil or false, your second statement won't execute do something to B.

And you forgot to handle corner cases like n = 4 and n = 25.

2/ In 99.9% of the case, you should choose the version that is easier to read/understand/debug. Unless this code is running 1000000 times/second, this won't matter. As you can see, you already created 2 potential bugs by trying to optimizing it prematurely :)

3/ In term of performances, the short answer is: it depends on your data.

The long answer is: You have to try to predict in what range n will be in most of the cases. You can just count the number of comparisons:

n < 2: Statement 1: 3 comparisons - Statement 2: 3 comparisons

2 < n <= 4: Statement 1: 3 comparisons - Statement 2: 2 comparisons

4 < n <= 25: Statement 1: 3 comparisons - Statement 2: 4 comparisons

25 < n: Statement 1: 3 comparisons - Statement 2: 5 comparisons

Upvotes: 2

Eric Jablow
Eric Jablow

Reputation: 7899

Well, the first choice reduces the possibility of logic errors. In the second choice, what happens if n == 4 or n == 25? Yes, you can fix that easily, but you'd have to recognize the possibility.

I assume the "do something" pseudocode blocks are just method calls, so you don't repeat your text much. It's a hard question to answer because neither method involves nested if-else clauses. The if clauses are so marginal in cost difference that I would not consider that, and the repetition in "do something" blocks are the same way. I can imagine a clearer way, however. Could A, B, and C all have a method like A.handle(n), B.handle(n), and C.handle(n), where the first does nothing if n <= 2, the second does nothing if n <= 4, and the third does nothing if n <= 25?

Upvotes: 0

Dan Wich
Dan Wich

Reputation: 4943

Assuming you can't predict a "most common path" (for example, knowing that n is between 2 and 4 the vast majority of the time), I would doubt that the second approach would be any more efficient; the number of comparisons made will be similar.

Upvotes: 0

Related Questions