Results 1 to 5 of 5

Thread: Which is better...? If... Else or Select Case? {Resolved}

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Resolved Which is better...? If... Else or Select Case? {Resolved}

    Just a simple question and I was wondering is anyone had any insight into this.

    If you are using lots of if-Elses or, lots of select cases - which would be faster.


    Cheers,

    RyanJ
    Last edited by sciguyryan; Jun 3rd, 2005 at 05:42 AM.
    My Blog.

    Ryan Jones.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Which is better...? If... Else or Select Case?

    I have heard valid arguments (and proof) for both being faster than the other, based on the situation in which they are used. Neither one is so much faster that there is an obvious choice without trying it out.

    Unless the code needs to be run at the fastest speed possible I would recommend just using whichever seems more appropriate at the time (based on the circumstances, your style, and which is easiest to read).

    If speed really matters then you should look into the structure of your code rather than just the actual language constructs that you implement it with. For example, due to the way that conditions are evaluated when they contain boolean And operators it can be much more efficient to use two If's instead (assuming that one of the conditions is used for another If), eg:
    VB Code:
    1. If (A And C) Then
    2.   'do this
    3. ElseIf (B) Then
    4.   'do that
    5. ElseIf (C) Then
    6.   'do something else
    7. Else....
    8. End If
    is more efficient like this:
    VB Code:
    1. If (C) Then
    2.   If (A) Then
    3.     'do this            (same number of conditions evaluated as before)
    4.   Else
    5.     'do something else  (one less condition evaluated)
    6.   End If
    7. ElseIf (B) Then
    8.   'do that              (one less condition evaluated)
    9. Else....               '(two less conditions evaluated)
    10. End If
    It isn't always possible to arrange repeated conditions in a collection of Ifs in a way like this, so it is worth considering putting the result of a calculation into a variable, then using the variable in the If's (but bear in mind this could be bad for speed as well as good).


    There are several useful tips (including one for Ifs/Select Cases) in this optimisation thread: http://www.vbforums.com/showthread.php?t=315632

  3. #3

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Which is better...? If... Else or Select Case?

    Ah, Right I see - thank you for the information si_the_geek

    Also, thanks for the thread link that should prove most useful!

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Which is better...? If... Else or Select Case? {Resolved}

    Actually I proved that If...ElseIf is faster than Select...Case in another thread (can't remember which one though). I think it won on both IDE mode and EXE mode. Even had the performance test uploaded in there. Try searching for "If Then" and see if you can find it. I think |2eM!x started that thread.

  5. #5

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Which is better...? If... Else or Select Case? {Resolved}

    Quote Originally Posted by Jacob Roman
    Actually I proved that If...ElseIf is faster than Select...Case in another thread (can't remember which one though). I think it won on both IDE mode and EXE mode. Even had the performance test uploaded in there. Try searching for "If Then" and see if you can find it. I think |2eM!x started that thread.

    Ok, Will do

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width