08-13-2022, 04:58 PM
Generally speaking, as long as you're not dealing with looping and such, it doesn't matter. For example:
SELECT CASE foo
CASE 1
'let's do lots of stuff
'and some optional stuff that only happens when foo is one
CASE 2
'let's do lots of stuff like above
'and some optional stuff that only happens when foo is two
END SELECT
Verses this:
'Let's do lots of stuff exactly as above
SELECT CASE foo
CASE 1
'we do the optional stuff for case 1
CASE 2
'we do the optional stuff for case 2
END SELECT
In both these situations, we're not going to see any real difference in program speed or performance. We still do the bunch of stuff no matter which branch we take with the SELECT CASE, so there's no savings in performance to come along anywhere. The second method produces less code and is generally easier to keep up with and debug, but performance wise, it's not going to be very different than the first method.
Now, where you *do* see changes in performance is when loops are involved.
FOR I = 1 to 1000
SELECT CASE Foo
CASE 1 'do some stuff if foo is 1
CASE 2 'do some stuff if foo is 2
END SELECT
NEXT
Verses the following:
SELECT CASE foo
CASE 1
FOR I = 1 TO !000
'Do some stuff for when foo is 1
NEXT
CASE 2
FOR I = 1 TO !000
'Do some stuff for when foo is 2
NEXT
END SELECT
In this situation, the second set of code will perform better than the first set of code, even though it's more code to it!! WHY?? Because it's only making that SELECT CASE decision once, whereas the first set of code has to make that decision 1000 different times. Structure here can play a huge part on performance, and is something to keep in mind if your code is running noticeably slower than desired.
SELECT CASE foo
CASE 1
'let's do lots of stuff
'and some optional stuff that only happens when foo is one
CASE 2
'let's do lots of stuff like above
'and some optional stuff that only happens when foo is two
END SELECT
Verses this:
'Let's do lots of stuff exactly as above
SELECT CASE foo
CASE 1
'we do the optional stuff for case 1
CASE 2
'we do the optional stuff for case 2
END SELECT
In both these situations, we're not going to see any real difference in program speed or performance. We still do the bunch of stuff no matter which branch we take with the SELECT CASE, so there's no savings in performance to come along anywhere. The second method produces less code and is generally easier to keep up with and debug, but performance wise, it's not going to be very different than the first method.
Now, where you *do* see changes in performance is when loops are involved.
FOR I = 1 to 1000
SELECT CASE Foo
CASE 1 'do some stuff if foo is 1
CASE 2 'do some stuff if foo is 2
END SELECT
NEXT
Verses the following:
SELECT CASE foo
CASE 1
FOR I = 1 TO !000
'Do some stuff for when foo is 1
NEXT
CASE 2
FOR I = 1 TO !000
'Do some stuff for when foo is 2
NEXT
END SELECT
In this situation, the second set of code will perform better than the first set of code, even though it's more code to it!! WHY?? Because it's only making that SELECT CASE decision once, whereas the first set of code has to make that decision 1000 different times. Structure here can play a huge part on performance, and is something to keep in mind if your code is running noticeably slower than desired.