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

Computersmith64

Well-Known Member
Licensed User
[UPDATED]

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:

B4X:
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)
Next
 
Log($"Average 1: ${tot1/1000}"$)
Log($"Average 2: ${tot2/1000}"$)
The result on my old Galaxy S3 was:
Average 1: 2258.509
Average 2: 2380.589
On my Nexus 5X it was:
Average 1: 654.841
Average 2: 655.312
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:

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

B4X:
If a = b Then
 ' nothing
Else
 Log("No")
End I
 

Computersmith64

Well-Known Member
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

B4X:
If a = b Then
 ' nothing
Else
 Log("No")
End I
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.
 

Star-Dust

Expert
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.
Yes "<>" will be equal to "=" as performance, obviously it was septically ironic it was not a real suggestion :p
 

Computersmith64

Well-Known Member
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

B4X:
If a = b Then
 ' nothing
Else
 Log("No")
End I
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.
 

Computersmith64

Well-Known Member
Licensed User
Yes "<>" will be equal to "=" as performance, obviously it was septically ironic it was not a real suggestion :p
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.
 

Computersmith64

Well-Known Member
Licensed User
The conclusion is that you will not gain any meaningful performance improvement from such small changes.
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.
 
Top