Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case or IF

Status
Not open for further replies.

rdavis

Programmer
Apr 29, 2002
157
US
Just a little post to get some opinions;

Which is quicker, Select Case or IF?

Thanks


Rob
 
I don't know, but if I were you, I would determine it empirically. Only someone with intimate knowledge of the compiler can answer this question. Most of those people are probably bound by NDAs. :)

Try compiling a project with nothing in it but a single module with a "Select Case" block. Then compile the analagous "Else If" implementation. The smaller one is probably faster. Of course, you could also do a simulation that calls the routines many times, and compare the execution times.

If you think about the two options, it seems that "Select Case" essentially does the same thing as "Else If" but is more compact to write. That is, once one of the conditions is satisfied, it does not check the others but goes right to the end of the block. Based on that reasoning, I would not be surprised if they compile to identical machine language.
 
I do not know if IF...THEN is faster than SELECT CASE, but I do know that:

SELECT CASE Variable

CASE 15

CASE 21

CASE 13

CASE ....

.....

END SELECT

is a lot easier to read and to maintain, than a corresponding IF ... ELSE statement. If you frequently have to adjust the code, then SELECT is the message.

By the way, if performance really is an issue, then it is good to know the frequency distribution of the Variable and to code the CASE statements in descending frequency order. The SELECT search is satisfied if the first match is found.



_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
You can't really make blanket statements about how one is "way faster" than the other -- a lot depends on the range of values that you're looking for and their datatypes (like rvBasic said).

I would create a small test app to run some sample data through about 100,000 times and time how long it takes.

Chip H.
 
In all I have read, Select Case is preferred if the number of conditions to be tested is 3 or more.
 
Did a test

The Select Case seems to be faster but only by .00007 secs for 100,000 loops

Just a thought

ps both apps compiled into 20kb applications-just incase anybody was wondering.

I have to agree though, the Case Select is a lot cleaner (and now it's faster too :) )


Just for fun
Scoty ::)

"Learn from others' mistakes. You could not live long enough to make them all yourself."
-- Hyman George Rickover (1900-86),
 
Personally, I like the Select case better also (easier to read). I just wanted to get everyone feel on this too, and I did. Thanks all.


Rob
 
The performance issue depends on how the compiler creates the jump vector for the case statement.

With an IF-THEN-ELSE statement, you know that execution will proceed checking the first condition, then the second, then the third, and so forth until a match is found, or you run out of statement. Therefore, you can optimize an IF-THEN-ELSE statement, putting the most likely matches at the top of the cascade. For example, consider the following statement:
Code:
If (Color = Red) Then
   do something
Else
   If (Color = Blue) Then
      do something
   Else
      If Color = Green) Then   
         do something
      End If
   End If
End If
If I know that 50% of my colors will be green, 30% will be blue and 20% will be red, then I have coded this if-then-else in the most inefficient and slowest possible manner. If you run your timing tests this way against the comparable case statment you will get one set of results. If you recode the IF-THEN-ELSE statement, changing the order of the checks, you will get a completely different set of results. Therefore without an understanding of both the statement setup and knowledge of the incoming data set, the test results may or may not be misleading, and certainly bring into question their conclusiveness.

Now Scoty - to fully appreciate the value of results, and to provide some credibility to the results, what was the distribution of the dataset being tested, what was the ordering of the conditions for the ITE statement, and what was the ordering for the case statement for that matter? And perhaps even more important, how clean was the testing environment? In other words, what other tasks the could have consumed processor time were running in the background, while the test was being conducted, thus skewing the results? I am not saying that you're results are incorrect, I just want to be sure that the results are in fact meaningful.

BTW, I will almost always use the case statement when dealing with a set of homogenious conditions.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
ok so im not sure how relevant this is... but...

i agree with CC, its all about how you structure your statements!!

but i would also like to bring attention to a line in MSDN (under If...Then...Else statement!!!)

Tip Select Case may be more useful when evaluating a single expression that has several possible actions.

like i said... how relevant this is is beyond me, but... hey its there now!!

good luck with whatever you decide!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
The Great Date Debate Thread222-368305
File Formats Galore @ or
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top