[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:
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.
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: