|
-
Oct 14th, 2001, 01:56 PM
#1
Thread Starter
Addicted Member
Which is faster, an if statement or making something = something else?
Okay, I know you are confused. Let me be more specific and we'll see if it helps.
Loop start
If X = False Then X = True (((((OR)))))) X = True
Loop end
With "If X = False Then X = True" it would only make it true once, which seems like it would save time because it wouldn't set X to true every time. But with "X = True" it would just go right ahead and make it true every time, which would seem to save time not having to check to see if X was equal to false every time. So, which one is faster? Checking to see if X = False, or making X = True?
Is it tired in here or is it just me?
Ryan Williams
-Using Vb6-
-
Oct 14th, 2001, 02:22 PM
#2
New Member
Simply X = True is much faster since the processor wouldn't have to jump trough your code.
X = True is only one CPU instruction, while
if x=false then x= true needs an extra test.
-
Oct 14th, 2001, 02:24 PM
#3
Frenzied Member
x = true would be faster in my opinion because it doesnt need to check anything
it just changes the value
where as the if statement slows down in the loopp
BEST WAY TO FIND OUT IS test it
write 2 loops
time them using GetTickCount API
i dont have vb installed yet (new format) or i would test it for you
-
Oct 14th, 2001, 02:28 PM
#4
Here is the test
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim lngStart As Long
Dim lngEnd As Long
Dim i As Long
Dim x As Boolean
Private Sub cmdEquals_Click()
x = True
lngStart = GetTickCount
For i = 1 To 2000000
x = True
Next i
lngEnd = GetTickCount
Debug.Print "with x = " & lngEnd - lngStart
End Sub
Private Sub cmdIf_Click()
x = True
lngStart = GetTickCount
For i = 1 To 2000000
If x = False Then
x = True
End If
Next i
lngEnd = GetTickCount
Debug.Print "with an if check " & lngEnd - lngStart
End Sub
-
Oct 14th, 2001, 02:34 PM
#5
New Member
The if structure in your second test is never taken; that is not a common situation. better try this:
Code:
dim Y as boolean
For i = 1 To 2000000
Y = Not Y
X = Y
If x = False Then
x = True
End If
Next i
and
Code:
dim Y as boolean
For i = 1 To 2000000
Y = Not Y
X = Y
x = True
Next i
This way, the if structure is taken 1000000 times and 1000000 times not.
-
Oct 14th, 2001, 02:40 PM
#6
Addicted Member
on my computer
this was 300
For i = 1 To 2000000
x = True
Next i
------------------------------------------
this was 426
For i = 1 To 2000000
If x = False Then
x = True
End If
Next i
-------------------------------------------
this was 298
For i = 1 To 2000000
If x = False Then x = True
Next i
so the If x = False Then x = True was faster :-)
Thanks For All Your Help!
If I helped you I give God the glory
From CodeGreen
-
Oct 14th, 2001, 02:42 PM
#7
The if structure in your second test is never taken; that is not a common situation. better try this:
Since when I ran the test, and it if statement was slower even when it was always false, I did not see a need to check further. However your way would probably be more acurate.
-
Oct 14th, 2001, 02:47 PM
#8
New Member
I think the results may change if the test program is compiled;
The two IF test (single line and multi line) should give the same results.
If the program isn't compiled, the interpreter has to read 3
lines instead of one. This is propably time consuming.
-
Oct 14th, 2001, 02:49 PM
#9
PowerPoster
x = true should be faster. if not, then VB has issues
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Oct 14th, 2001, 02:50 PM
#10
Addicted Member
if x is most of the time going to be true then
If x = False Then x = True
will be faster
but if x most of the time is going to be false then
x = true
will be faster
Thanks For All Your Help!
If I helped you I give God the glory
From CodeGreen
-
Oct 14th, 2001, 02:55 PM
#11
I think you are wrong. Any time vb is going to have to check a value it will probably take longer than to just change it. Either way, if you are only talking a few milliseconds with a loop that runs two million time, what difference does it make.
-
Oct 14th, 2001, 02:56 PM
#12
Addicted Member
after you have compiled it there all about the same :-)
Thanks For All Your Help!
If I helped you I give God the glory
From CodeGreen
-
Oct 14th, 2001, 02:59 PM
#13
Addicted Member
can some one help with this
Help Here
Thanks For All Your Help!
If I helped you I give God the glory
From CodeGreen
-
Oct 14th, 2001, 03:10 PM
#14
Originally posted by codegreen
if x is most of the time going to be true then
If x = False Then x = True
will be faster
but if x most of the time is going to be false then
x = true
will be faster
If X is always true, they should take about the same time. If X is ever false, doing it without the If statement would be faster. Since the only reason why you'd use the If statement is because X might be one or the other, it would be more practical to simply put "X = True".
-
Oct 14th, 2001, 03:14 PM
#15
Frenzied Member
Re: Which is faster, an if statement or making something = something else?
Originally posted by krah
Okay, I know you are confused. Let me be more specific and we'll see if it helps.
Loop start
If X = False Then X = True (((((OR)))))) X = True
Loop end
With "If X = False Then X = True" it would only make it true once, which seems like it would save time because it wouldn't set X to true every time. But with "X = True" it would just go right ahead and make it true every time, which would seem to save time not having to check to see if X was equal to false every time. So, which one is faster? Checking to see if X = False, or making X = True?
Something i'm wondering though...If you only check for one thing, then after you find it, shouldn't you break out of the loop? If its a For...Next loop then use Exit For. Not sure about the other loops. Just a thought.
You just proved that sig advertisements work.
-
Oct 14th, 2001, 03:27 PM
#16
Thread Starter
Addicted Member
That would be true if I was only checking for one thing, and it looks like that in my example, but I did that just for the sake of simplicity. There is actually quite a bit more jammed in there.
Is it tired in here or is it just me?
Ryan Williams
-Using Vb6-
-
Oct 14th, 2001, 03:30 PM
#17
Fanatic Member
On my computer this is what I got.
with x = 170
with an if check 200
equals seems to be faster than an if statement.
-
Oct 14th, 2001, 03:33 PM
#18
Frenzied Member
Originally posted by krah
That would be true if I was only checking for one thing, and it looks like that in my example, but I did that just for the sake of simplicity. There is actually quite a bit more jammed in there.
Ok, cool.
You just proved that sig advertisements work.
-
Oct 14th, 2001, 03:35 PM
#19
Frenzied Member
I think you need to keep in mind also that windows is multi-tasking. You could probably run that test several times in a row and get different results because the CPU is not dedicated to your test program. Even if no other applications are running windows still has background stuff going on.
Greg
Free VB Add-In - The Reference Librarian
Click Here for screen shot and download link.
-
Oct 14th, 2001, 07:24 PM
#20
Frenzied Member
run this in compiled form and tell the numbers
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim lngStart As Long
Dim lngEnd As Long
Dim i As Long
Dim x As Boolean
Private Sub com1()
x = True
lngStart = GetTickCount
For i = 1 To 2000000
x = True
Next i
lngEnd = GetTickCount
Print "with x = " & lngEnd - lngStart
End Sub
Private Sub com2()
x = True
lngStart = GetTickCount
For i = 1 To 2000000
If x = False Then
x = True
End If
Next i
lngEnd = GetTickCount
Print "with an if 3 line check " & lngEnd - lngStart
End Sub
Private Sub com3()
x = True
lngStart = GetTickCount
For i = 1 To 2000000
If x = False Then x = True
Next i
lngEnd = GetTickCount
Print "with an if 1 line check " & lngEnd - lngStart
End Sub
Private Sub Form_Load()
Form1.Show
DoEvents
com1
com2
com3
End Sub
-
Oct 14th, 2001, 07:35 PM
#21
PowerPoster
Hi
As Tygur said, it is essentially a moot point. If u didnt need to perform the test then u would put x = true BUT if the effect of setting X = true directly causes a noticeable performance downgrade then u would do the test. A good example of that is when u are doing things in a MouseOver event. it may cause noticeable flicker to continually set a label's properties rather than testing whether a property is set or not.
regards
Stuart
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
|