If...Then...Else or Select Case Statement 
Author Message
 If...Then...Else or Select Case Statement

        If ... ElseIf is faster.

--
Jon Clegg



Quote:
> Which is faster?
> ---------------------------------------
> Select Case TempA
> Case 1
>    .
>    .
> Case 2
>    .
>    .
> Case 3
>    .
>    .
> End Select
> ----------------------------------------
>                    or
> ----------------------------------------
> If TempA = 1 then
>    .
>    .
> Elseif TempA = 2 then
>    .
>    .
> Elseif TempA = 3 then
>    .
>    .
> End if



Mon, 22 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

Quote:

>         If ... ElseIf is faster.

> --
> Jon Clegg



> > Which is faster?
> > ---------------------------------------
> > Select Case TempA
> > Case 1
> >       .
> >       .
> > Case 2
> >       .
> >       .
> > Case 3
> >       .
> >       .
> > End Select
> > ----------------------------------------
> >                    or
> > ----------------------------------------
> > If TempA = 1 then
> >       .
> >       .
> > Elseif TempA = 2 then
> >       .
> >       .
> > Elseif TempA = 3 then
> >       .
> >       .
> > End if

Only when you are comparing a few items.  While If then does run faster
if you are comparing quite a few things Select Case is faster.  The
reason is the comparision is only done once.  Also your code is easier
to read and maintain with Select case vs. If then.


Mon, 22 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

Which is faster?
---------------------------------------
Select Case TempA
Case 1
        .
        .
Case 2
        .
        .
Case 3
        .
        .
End Select
----------------------------------------
                   or
----------------------------------------
If TempA = 1 then
        .
        .
Elseif TempA = 2 then
        .
        .
Elseif TempA = 3 then
        .
        .
End if



Tue, 23 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

Quote:

> Also your code is easier
>to read and maintain with Select case vs. If then.

I just spent time with a printout and marking pen at work
trying to decipher code built like:

if flag=0 then
  ' page of stuff
end if

if flag=1 then
  ' page of stuff
end if

if flag=2 then
  ' page of stuff
end if

if flag=3 then
  ' page of stuff
end if

Sheesh. Drove home yelling "Case Structure!" at passing cars ;)

- Matt



Tue, 23 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

Quote:

>Only when you are comparing a few items.  While If then does run faster
>if you are comparing quite a few things Select Case is faster.  The
>reason is the comparision is only done once.  Also your code is easier
>to read and maintain with Select case vs. If then.

Also note that if you have a case that occurs more often than the others it is
best to put it at the top of the case list because VB will go through each one
without an index to find the one that it is looking for.  You are right
though, a select statement is more efficient for more than 3 or so items and
is much easier to read if well-indented.  In an If-Then-ElseIf-Then... the
select variable is referenced every time.

Brad

_______________________________________________________________________
 Bradley S. Murray       Princeton Computer Consulting  (609) 730-9100
 BradMurray(at)usa.net     Pennington, NJ 08534-3612    Fax:  275-5651

To respond, use the address in the signature and while you're at it,
dummy up your reply address to avoid getting junk e-mail from these
scum bags who scan newsgroups to build address lists

Support the anti-Spam amendment - Join at http://www.cauce.org/



Tue, 23 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement


09:11:37 +1200):

Quote:
>Which is faster?
>Select Case TempA
yes
>                   or
>If TempA = 1 then

=> no

Mathias Schiffer, Germany
-----------------------------------------------
VB-FAQ: http://home.sol.no/jansh/vb/default.htm
NG-tips: http://www.rat.de/metty/vbnews



Tue, 23 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

Quote:

>Which is faster?
>---------------------------------------
>Select Case TempA
>    .
>End Select
>If TempA = 1 then
>    .
>End If

It isn't really matter of speed.  The amount of time it takes to
execute either one is insignificant.  An extra millisecond isn't going
to make or break your program.

You should be more concerned with maintenance.

I've seen more code where the programmer wrote speghetti in the name
of preformance. (specificallyVB3 and comments)

Remember that your are probably not the only one who will ever see the
code.

-David Lanouette
-DLanouette at Techie dot com
-Free advise is worth what you pay for it.

-David Lanouette
-DLanouette at Techie dot com
-Free advise is worth what you pay for it.



Wed, 24 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

Nope nope nope.  Select is a lot faster and rquires a lot less
processor work than 'if then else' statements.  If you have many tests,
changing 'if then else's to a single Select, or nested switches
will save a whole lotta time and werk on your processor. A Select
makes one test, while an 'if else' requires two.  If you have
a lotta ElseIfs you need to change to a switch statement. Write it
both ways and then step though your code with F8. You'll see that Select
is a lot more efficient.

Also, you're more likely to end up with spaghetti code if you use if
then elseif else code.

Quote:


> >Which is faster?
> >---------------------------------------
> >Select Case TempA
or
> >If TempA = 1 then
> >End If

> It isn't really matter of speed.  etc.

> You should be more concerned with maintenance.

> I've seen more code where the programmer wrote speghetti in the name
> of preformance. (specificallyVB3 and comments)

--
Len Goforth  aka SlowHandd

--------------------------------------------------------
Angels can fly because they take themselves lightly.
It is never too late to have a happy childhood.


Thu, 25 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement



Quote:
>Which is faster?
>---------------------------------------
>Select Case TempA
>Case 1
<snip>
>----------------------------------------
>                   or
>----------------------------------------
>If TempA = 1 then
>    .
>    .
>Elseif TempA = 2 then

<snip>

For once through, you won't notice a difference in speed.  I'd go with
either one... whichever is easier for you to debug a year from now.  

I'd prefer Select Case if you're only checking a single variable for a
single value.  However, if you've got a bunch of "is x greater than y
but less than z" elseif "x greater than y and greater than z"...
Then the if/elseif is almost necessary.



Thu, 25 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

Hello,
I am not sure which is faster. I have a simple rule of thumb that I follow.
Although it is more for readability. If you have to use an ElseIf statement
then use the Select case instead. It will make your code a lot more
readable. Besides you can do multiple compares in the select case I.E.
        Case 1 to 10
        case 1,3,5,7,11,13      'primes...

It also makes for expanding the statement a whole lot easier.

Cheers,
Mike-Ro.



Quote:
> According to the Visual Basic Certification Prepartion Manual, the
> Select Case is faster.

[CLIP]


Sat, 27 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement


Quote:


>>Which is faster?
> [snip]
> Hi Morris,
> why not asking the current time before and after executing the code?
> If the time is too short, to see any difference, just put the code
> into a loop.

> Gerhard Amberger, Germany

Actually if the time is too short then who cares???

D.M. (Mike) Mattix
Monsanto Company
Luling, LA

(My comments do not reflect the position of Monsanto)
(I know, like you thought they did..)



Tue, 30 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement


Quote:


>>Which is faster?
> [snip]
> Hi Morris,
> why not asking the current time before and after executing the code?
> If the time is too short, to see any difference, just put the code
> into a loop.

> Gerhard Amberger, Germany

Sorry about the previous post's snide comment.  I actually like the
code following ability of the CASE statement over a bunch of IF's,
ELSE's and END IF's all over the place.  The if statement has it's
place if you are doing binary compares (True/False).  If you are
doing Multiple Choice type compares (Do A,B,C, or D) then the
CASE statement really shines in the readability sense.

And as my previous post indicated if the difference is so short that
you can not tell from the Now function (and I believe it will be..) then
who cares??

D.M. (Mike) Mattix
Monsanto Company
Luling, LA

(My comments do not reflect the position of Monsanto)
(I know, like you thought they did..)



Tue, 30 Nov 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement

I might be wrong, but i think that the case becomes more efficien only when
a large number of conditions is applied.
If only 3 conditions are tested... as in your example, im pretty sure that
IF works faster then CASE



Quote:
> Which is faster?
> ---------------------------------------
> Select Case TempA
> Case 1
>    .
>    .
> Case 2
>    .
>    .
> Case 3
>    .
>    .
> End Select
> ----------------------------------------
>                    or
> ----------------------------------------
> If TempA = 1 then
>    .
>    .
> Elseif TempA = 2 then
>    .
>    .
> Elseif TempA = 3 then
>    .
>    .
> End if



Mon, 06 Dec 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement


Mattix) schreibt:

Quote:
>Actually if the time is too short then who cares???

That's a practical question ;-)

There is one substantial difference in the code, created by the compiler:
The case value is evaluated only once, then compared to the values. The If
statements always must evaluate both values before comparison.

For speed optimization, check for the most common cases first, and if you
prefere If's, use ElseIf for subsequent test.

DoDi



Thu, 09 Dec 1999 03:00:00 GMT  
 If...Then...Else or Select Case Statement



Quote:
>If only 3 conditions are tested... as in your example, im pretty sure
that
>IF works faster then CASE

Sorry, you're wrong. As I outlined before, every If requires access to 2
expressions, whereas in the first expression is evaluated in Select, and
afterwards only the comparison value is evaluated and compared in every
Case line.

In your example, an On ... Goto would be the fastest solution, if the
range of values is somewhat contiguous, else the line would be very long.

DoDi



Sun, 12 Dec 1999 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Embedded or nested Select Case statements

2. Using a wildcard string comparison in a select case statement

3. Can you nest Select Case statements?

4. Problem with Expression in a Select Case Statement

5. Can you help convert IF Statement to a Select Case Statement?

6. VBScript Select Case vs VB Select Case

7. Select Case Statement Help

8. Select Case Statement

9. Criteria for select case statements

10. Select Case statement is driving me mad!

11. Select Case Statement

12. If statement in select case problem

 

 
Powered by phpBB® Forum Software