andieje
asked on
what's quicker? equal to or not equal to
Hi
This is a question for my curiosity.
Lets say i wanted to perform an action (Action a) if a particular variable was equal to a value of x and perform another action (action b) otherwise. I could do this 2 ways
If var = x then
action a
else
action b
end if
if var != x
action b
else
action a
end if
my common sense would tell me the first is quicker but computers have been known to do things i don't understand for optimisation reasons. Which one is quicker?
thankd
andrea
This is a question for my curiosity.
Lets say i wanted to perform an action (Action a) if a particular variable was equal to a value of x and perform another action (action b) otherwise. I could do this 2 ways
If var = x then
action a
else
action b
end if
if var != x
action b
else
action a
end if
my common sense would tell me the first is quicker but computers have been known to do things i don't understand for optimisation reasons. Which one is quicker?
thankd
andrea
for string comparison you'll have to use String.Compare
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what language are yoo using though?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi andieje;
Here is some test code to test the speed of each version using VS .Net 2005.
My test results where based on 100,000,000 times through the loop on each.
The Eauals Version
Numeric code run time 460 ms
String code run time 1848 ms
The Not Eauals Version
Numeric code run time 470 ms
String code run time 2798 ms
Sample Code
Dim sw1 As New Stopwatch
Dim sw2 As New Stopwatch
Dim Maxloops As Integer = 100000000
Dim var1 As Integer = 100
Dim Var2 As String = "ABC"
Dim msg As String
sw1.Reset()
sw1.Start()
For idx As Integer = 0 To Maxloops
If var1 = 100 Then
var1 += 1
Else
var1 -= 1
End If
Next
sw1.Stop()
sw2.Reset()
sw2.Start()
For idx As Integer = 0 To Maxloops
If Var2 = "ABC" Then
Var2 = "ABCD"
Else
Var2 = "ABC"
End If
Next
sw2.Stop()
msg = "Numeric code run time " & sw1.ElapsedMilliseconds.To String & " ms" & vbCrLf & _
"String code run time " & sw2.ElapsedMilliseconds.To String & " ms"
MessageBox.Show(msg, "Equals Version")
sw1.Reset()
sw1.Start()
For idx As Integer = 0 To Maxloops
If var1 <> 100 Then
var1 += 1
Else
var1 -= 1
End If
Next
sw1.Stop()
sw2.Reset()
sw2.Start()
For idx As Integer = 0 To Maxloops
If Var2 <> "ABC" Then
Var2 = "ABCD"
Else
Var2 = "ABC"
End If
Next
sw2.Stop()
msg = "Numeric code run time " & sw1.ElapsedMilliseconds.To String & " ms" & vbCrLf & _
"String code run time " & sw2.ElapsedMilliseconds.To String & " ms"
MessageBox.Show(msg, "Not Equals Version")
Fernando
Here is some test code to test the speed of each version using VS .Net 2005.
My test results where based on 100,000,000 times through the loop on each.
The Eauals Version
Numeric code run time 460 ms
String code run time 1848 ms
The Not Eauals Version
Numeric code run time 470 ms
String code run time 2798 ms
Sample Code
Dim sw1 As New Stopwatch
Dim sw2 As New Stopwatch
Dim Maxloops As Integer = 100000000
Dim var1 As Integer = 100
Dim Var2 As String = "ABC"
Dim msg As String
sw1.Reset()
sw1.Start()
For idx As Integer = 0 To Maxloops
If var1 = 100 Then
var1 += 1
Else
var1 -= 1
End If
Next
sw1.Stop()
sw2.Reset()
sw2.Start()
For idx As Integer = 0 To Maxloops
If Var2 = "ABC" Then
Var2 = "ABCD"
Else
Var2 = "ABC"
End If
Next
sw2.Stop()
msg = "Numeric code run time " & sw1.ElapsedMilliseconds.To
"String code run time " & sw2.ElapsedMilliseconds.To
MessageBox.Show(msg, "Equals Version")
sw1.Reset()
sw1.Start()
For idx As Integer = 0 To Maxloops
If var1 <> 100 Then
var1 += 1
Else
var1 -= 1
End If
Next
sw1.Stop()
sw2.Reset()
sw2.Start()
For idx As Integer = 0 To Maxloops
If Var2 <> "ABC" Then
Var2 = "ABCD"
Else
Var2 = "ABC"
End If
Next
sw2.Stop()
msg = "Numeric code run time " & sw1.ElapsedMilliseconds.To
"String code run time " & sw2.ElapsedMilliseconds.To
MessageBox.Show(msg, "Not Equals Version")
Fernando
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Fernando
I agree that the difference that I reported on the Numerical side looked odd, so I've been experimenting a bit. So far as I can tell, it arises from the StopWatch. The "real" results should be those you show: that is, that there should be no appreciable difference between testing for Equal or Not Equal.
It seems clear that - on my system at least - StopWatch.Stop doesn't always take immediate effect. I haven't yet worked out the distinguishing features between when it does work OK and when it doesn't and, given your results, it does seem to be system-dependent at least to some extent.
I personally still tend to use Ticks, so this is a phenomenon I hadn't noticed before. But I'll dig into it a bit more and post here again if I have any positive findings.
Roger
I agree that the difference that I reported on the Numerical side looked odd, so I've been experimenting a bit. So far as I can tell, it arises from the StopWatch. The "real" results should be those you show: that is, that there should be no appreciable difference between testing for Equal or Not Equal.
It seems clear that - on my system at least - StopWatch.Stop doesn't always take immediate effect. I haven't yet worked out the distinguishing features between when it does work OK and when it doesn't and, given your results, it does seem to be system-dependent at least to some extent.
I personally still tend to use Ticks, so this is a phenomenon I hadn't noticed before. But I'll dig into it a bit more and post here again if I have any positive findings.
Roger
Thanks Roger for your efforts.
Fernando
Fernando
Fernando
This has really raised a completely different issue, so I've posted a specific question. It's here
https://www.experts-exchange.com/questions/22047746/VB-NET-2005-StopWatch-interference-by-MessageBox.html
Roger
This has really raised a completely different issue, so I've posted a specific question. It's here
https://www.experts-exchange.com/questions/22047746/VB-NET-2005-StopWatch-interference-by-MessageBox.html
Roger
ASKER
Thanks very much everyone. That was an interesting question
Always willing to help out. ;=)
Hi andieje;
Is there anything else that we can help with this question?
Fernando
Is there anything else that we can help with this question?
Fernando
ASKER
no, it was just for my curiosity :)
but i need your help with my processes again!!! please let me know when you around and i will post a question. i have posted a few before but you werent around so they were ignored :(
but i need your help with my processes again!!! please let me know when you around and i will post a question. i have posted a few before but you werent around so they were ignored :(
Hi andieje;
I am generally around some part of each day.
Fernando
I am generally around some part of each day.
Fernando
ASKER