|
-
Apr 5th, 2002, 02:51 PM
#1
Thread Starter
PowerPoster
Which is Faster and Why?
I already know the Answer but i am just curious to see how well known this fact is about the VB compiler.
If i write this code 2 different ways which will be faster once it's compiled?
A)
VB Code:
If iConditionOne < 1 Then
If iConditionTwo < 20 Then
'DoSomething
End if
End if
B)
VB Code:
If iConditionOne < 1 And iConditionTwo < 20 Then 'DoSomething
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 5th, 2002, 02:53 PM
#2
Hyperactive Member
The first because it evaluates one at a time?????
just a guess.
-
Apr 5th, 2002, 02:54 PM
#3
Without actually KNOWING the answer, I would say the first.
The second has three evaluations to perform, whereas the first only has two.
-
Apr 5th, 2002, 02:56 PM
#4
Hyperactive Member
B is quicker.
Using method A, VB has to ... dunno if right word " Re-Itterate " Each entry...
It has to trigger the first value before it even looks at the second one....
Method B already states the the 2 values together to look for.. whatever..
I'm right arent i? Just cant explain it very well
-
Apr 5th, 2002, 02:59 PM
#5
Me thought is such:
Case A
Eval First
Its true
Eval Second
Its true
. . Do processing
Eval First
Its False
Skip over
Case B
Eval First
Its true
Eval Second
Its true
Eval true and true
Its true
. . Do processing
Eval First
Its False
Eval Second
Its True
Eval true and false
Its False
Skip over
-
Apr 5th, 2002, 03:03 PM
#6
i would say it doesn't matter unless you have some really complex statements... i don't think you would ever see a difference using the example you put up.. even though i am sure it was just an example... the computer is too fast for you to see any difference.. even an old computer... but you know what i am saying right?
-
Apr 5th, 2002, 03:04 PM
#7
Black Cat
I say A as well, as VB6 does not support short-circuiting, IIRC.
Josh
Get these: Mozilla Opera OpenBSD
I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.
-
Apr 5th, 2002, 03:07 PM
#8
Hyperactive Member
My version of thought was:
Method A:
Search through Everything for First Value
If true Record value and continue to end
Search Through Everything for Second Value
Result
Method B:
Search through Everything for First And Second value near-Simaltoneously
no?
-
Apr 5th, 2002, 03:07 PM
#9
PowerPoster
Well
A is. Somewhere i read it take more time for VB to evaluate with "and" ... even though it's still eval 2 values.
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Apr 5th, 2002, 03:16 PM
#10
Hyperactive Member
fine :P
I conceed
it's Eh
-
Apr 5th, 2002, 03:18 PM
#11
Thread Starter
PowerPoster
JoshT got it correct.
A is faster becasue VB does not use Short Circuiting.
Short Circuiting means that if ConditionOne evaluates to false then it doesnt even look at the rest of the code. But VB however, does look at the rest of the code on that line.
This example alone would not make any difference in overall speed of a project. But if you make a habbit of using Optimization techniques such as this throughout an entire project you will indeed notice a difference.
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 5th, 2002, 03:29 PM
#12
Banned
That's correct. We've learned at school that the use of nested if statements can increase speed very much! (Not to mistake it for nested loops, which decreases speed exponently (is that correct?))
-
Apr 5th, 2002, 03:30 PM
#13
-
Apr 5th, 2002, 03:33 PM
#14
Originally posted by Michael_Kamen
That's correct. We've learned at school that the use of nested if statements can increase speed very much! (Not to mistake it for nested loops, which decreases speed exponently (is that correct?))
Not really. It's just that you can end up doing many, many loops, which take a while.
-
Apr 5th, 2002, 03:35 PM
#15
Banned
Originally posted by kleinma
so what does he win???
Option A
But i think you will find it hard to measure any difference, until you're going to use this kind of statements hundreds of times.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|