We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

what's quicker? equal to or not equal to

andieje
andieje asked
on
Medium Priority
585 Views
Last Modified: 2010-04-23
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
Comment
Watch Question

Author

Commented:
Perhaps it's also different depending on what you're comparing. Perhaps in  a string comparison number 2 is quicker .
CERTIFIED EXPERT

Commented:
for string comparison you'll have to use String.Compare
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
With modern CPU, the answer is: put the condition the way so it turns true the most times.

reason:
  CPU today compute in advance, while on IF's, they continue in advance on the most probable way.
  usually, that is the first one in IF, so the condition to evaluate to true.
  if the condition evaluates to the other value (false), then the code that has already been computed in advance will be thrown away in the CPU cache and the other part will be avaluated.

in most IF conditions it does not really matter, though.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
what language are yoo using though?
CERTIFIED EXPERT
Commented:
example:

Dim str1 As String = "ppp"
Dim str2 As String = "ccc"
Dim res As Int16 = String.Compare(str1, str2)
Console.WriteLine("First result:" + res.ToString())
str2 = "ttt"
res = String.Compare(str1, str2)
Console.WriteLine("Second result:" + res.ToString())
str1 = "ttt"
res = String.Compare(str1, str2)
Console.WriteLine("Third result:" + res.ToString())
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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
Commented:
Fernando

I think the logic of the testing code may have gone a bit wrong.  I think the idea is that it should toggle the tested value so that it is true and false on alternate passes through the loop.  It does that on the Equals version.  But to make it do it on the Not Equals version, the operations need to be put the other way round from the Equal 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 = "ABC"
            Else
                Var2 = "ABCD"
            End If
        Next
        sw2.Stop()

I've tried it with that, and the results are significantly different:-

Equals Version
Numeric code run time 354 ms
String code run time 1654 ms
Not Equals Version
Numeric code run time 1104 ms
String code run time 1648 ms

Equals Version
Numeric code run time 362 ms
String code run time 1632 ms
Not Equals Version
Numeric code run time 1181 ms
String code run time 1623 ms

Equals Version
Numeric code run time 363 ms
String code run time 1640 ms
Not Equals Version
Numeric code run time 1089 ms
String code run time 1627 ms

Equals Version
Numeric code run time 357 ms
String code run time 1629 ms
Not Equals Version
Numeric code run time 1146 ms
String code run time 1658 ms

For strings, it doesn't seem to make any real difference.  But for integers, it looks like a factor of about three.

Does my revision make sense?  Or have I missed something?

Roger
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Hi Roger;

You are correct about the Not Equals version. When I wrote the Equals version I copied and past the code charged the var names to reflect for the new test but neglected to change the test around. Having said that I would not have expected to see much of a difference in the test results because it is doing one comparison and one assignment through each loop.

 My new test results with Rogers correction:

Equals Version
Numeric code run time 455 ms
String code run time 1867 ms
Not Equals Version
Numeric code run time 457 ms
String code run time 1916 ms

Equals Version
Numeric code run time 459 ms
String code run time 1872 ms
Not Equals Version
Numeric code run time 458 ms
String code run time 1909 ms

Equals Version
Numeric code run time 459 ms
String code run time 1855 ms
Not Equals Version
Numeric code run time 473 ms
String code run time 1864 ms

Equals Version
Numeric code run time 453 ms
String code run time 1870 ms
Not Equals Version
Numeric code run time 465 ms
String code run time 1880 ms

I do not know how to explain the different in results between what you got and what I just got.

Thank you for keeping me honest. I do not like posting bad code and I should have given it a closer look.

Fernando ;=)

Commented:
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
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Thanks Roger for your efforts.

Fernando

Commented:
Fernando

This has really raised a completely different issue, so I've posted a specific question.  It's here

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22047746.html

Roger

Author

Commented:
Thanks very much everyone. That was an interesting question
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Always willing to help out. ;=)
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi andieje;

Is there anything else that we can help with this question?

Fernando

Author

Commented:
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 :(
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi andieje;

I am generally around some part of each day.

Fernando
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.