Link to home
Start Free TrialLog in
Avatar of andieje
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
Avatar of andieje
andieje

ASKER

Perhaps it's also different depending on what you're comparing. Perhaps in  a string comparison number 2 is quicker .
Avatar of YZlat
for string comparison you'll have to use String.Compare
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what language are yoo using though?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.ToString & " ms" & vbCrLf & _
            "String code run time " & sw2.ElapsedMilliseconds.ToString & " 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.ToString & " ms" & vbCrLf & _
            "String code run time " & sw2.ElapsedMilliseconds.ToString & " ms"
        MessageBox.Show(msg, "Not Equals Version")


Fernando
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks Roger for your efforts.

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
Avatar of andieje

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
Avatar of andieje

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 :(
Hi andieje;

I am generally around some part of each day.

Fernando