Android Example [TEST RESULT] a <> b vs. Not(a = b)

Discussion in 'Tutorials & Examples' started by Computersmith64, Jul 10, 2018.

  1. Computersmith64

    Computersmith64 Well-Known Member Licensed User


    Lately I've fallen into the habit of using Not instead of <> for Boolean comparisons - eg: Not(a = b) instead of a <> b. I'm not sure why I started doing this - probably because I think it looks tidier...

    Anyway it occurred to me that one method might be less efficient than the other, so I decided to run a test to find out. I used the code below to run 1,000 loops of each method to compare the average execution time:

    Private nt As NanoTime
    Private i As Int
    Private tot1, tot2 As Long
    Private startTime1, startTime2 As Long
    For i = 0 To 999
        startTime1 = nt.NanoTime
    If a <> b Then

    End If
        tot1 = tot1 + (nt.NanoTime - startTime1)
        startTime2 = nt.NanoTime
    If Not(a = b) Then

    End If
        tot2 = tot2 + (nt.NanoTime - startTime2)
    Log($"Average 1: ${tot1/1000}"$)
    Log($"Average 2: ${tot2/1000}"$)
    The result on my old Galaxy S3 was:
    On my Nexus 5X it was:
    This was running in release mode & shows that there is very little difference between them. My previous tests in debug mode showed a significant difference on the S3, with the Not() method being a lot slower than the <> method.

    - Colin.
    Last edited: Jul 10, 2018
    hatzisn likes this.
  2. Star-Dust

    Star-Dust Expert Licensed User

    Give it a try, instead of working on "A <> B" or "Not (A = B)" (which are 2 operations) try with the ELSE.
    and see the difference with the other two methods :p

    If a = b Then
    ' nothing
    End I
  3. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    Actually Not(a = b) is 2 operations, but a <> b isn't - or at least it's no different to a = b operation-wise.

    I've updated the result, because the first tests I ran were in debug mode. When I re-ran them in release mode the execution was a lot faster & there wasn't any significant difference between the 2 methods. I also took the log statement out because it would be creating some overhead that would vary significantly from device to device.

    I'll run a test using your suggestion & post the results, but even if it's significantly faster I wouldn't use it (unless I really, really needed to optimize the app) because I'd end up with a heap of redundant if a = b statements.

    - Colin.
  4. Star-Dust

    Star-Dust Expert Licensed User

    Yes "<>" will be equal to "=" as performance, obviously it was septically ironic it was not a real suggestion :p
    Computersmith64 likes this.
  5. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    So I ran the test using your code & on the S3 it executes in about half the time, but on the Nexus it's pretty much the same (a little bit quicker). So I guess it is more efficient execution-wise, but not so much coding-wise. :)

    - Colin.
  6. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    Hah! But my test showed that the "clunky" If-Then-Else statement is actually faster! So the conclusion is that ugly code = fast code! :p

    - Colin.
    hatzisn and Star-Dust like this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    The conclusion is that you will not gain any meaningful performance improvement from such small changes.

  8. LucaMs

    LucaMs Expert Licensed User

    If Not(...

    is also more readable!
    Computersmith64 likes this.
  9. sorex

    sorex Expert Licensed User



    for me atleast. if you change things later to <, <=, > or >= you have more editing to do.
  10. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    Thanks Erel - I absolutely agree with the points in your video relating to simplicity & optimization. I always try to make my code as simple & readable as possible & generally don't worry about optimization unless I can see there's a performance issue.

    In this case, I was interested to see if there is a performance hit using Not() instead of <> because I use Not() a lot for Boolean vars (ie: Not(isThisTrue) vs. isThisTrue = False) & wondered if I was inadvertently writing relatively slower code by also using Not() for comparing other types. I figured I'd share the results in case anyone else was wondering the same thing.

    - Colin.
    Peter Simpson and Erel like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice