• Welcome to Valhalla Legends Archive.
 

Case as to If?

Started by ObsidianWolf, November 26, 2003, 10:18 AM

Previous topic - Next topic

What do you prefer using within a program?  If or Select Case?

If
1 (5%)
Select Case
8 (40%)
Each in its own place
11 (55%)

Total Members Voted: 13

Grok

There's nothing natural about If statements.

Skywing

#16
In a language like C or C++ that imposes strict requirements on what you can have for a switch (analog of "select case"), a multiple-selection flow structure is generally much faster than a series of if statements.

This is because the compiler can often optimize the multiple-selection structure to use only one or two compares, despite there being multiple outcomes, something that generally isn't possible with if statements.

Consider the following:


int i;
// ...
switch(i) {

case 0: // ...
break;
case 1: // ...
break;
case 2: case 3: //  ...
break;
case 4: // ...
break;

}


The compiler can be clever and turn this into a table lookup that goes something like this in pseudocode:

handlerindex = handlerindextable;
handler = handlertable[handlerindex];
goto handler;

With this kind of optimization, there is no need to individually test for outcomes.

Note that because VB allows you to put almost anything into a select case construction, this optimization is probably not as applicable, so the speed difference between an if and a select case may not be as drastic as the speed difference between an if and a switch in C or C++.

hismajesty

Quote from: Fleet- on December 10, 2003, 07:32 AM
I was told by "professionals", that if statements were naturally quicker, is this true?

AFAIK no. When I first started in VB I did If statements for everything and was told that Case is better. Where exactly did these "professionals" work? Also, I may be wrong (and probably am) so I too would like to know. :P

Spht

#18
Quote from: hismajesty on December 10, 2003, 02:43 PM
Quote from: Fleet- on December 10, 2003, 07:32 AM
I was told by "professionals", that if statements were naturally quicker, is this true?

AFAIK no. When I first started in VB I did If statements for everything and was told that Case is better. Where exactly did these "professionals" work? Also, I may be wrong (and probably am) so I too would like to know. :P

Work with the logic which I posted and you'll be fine.

Adron

Quote from: Grok on December 10, 2003, 01:23 PM
There's nothing natural about If statements.

They sound like natural language!

Etheran

I thought polymorphism was invented to rid the world of the cases!  ;D

hismajesty

Quote from: Spht on December 10, 2003, 02:56 PM
Quote from: hismajesty on December 10, 2003, 02:43 PM
Quote from: Fleet- on December 10, 2003, 07:32 AM
I was told by "professionals", that if statements were naturally quicker, is this true?

AFAIK no. When I first started in VB I did If statements for everything and was told that Case is better. Where exactly did these "professionals" work? Also, I may be wrong (and probably am) so I too would like to know. :P

Work with the logic which I posted and you'll be fine.

Aye, so I was correct?