Other If Not(...

wonder

Expert
Licensed User
Longtime User
This has been know for a while, but thanks for bringing it up... :)

For mass replacement, press F3, and Replace "Not(" with "False = (".
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I find posts like this to be more harmful than useful.

1. In 99.999% of the cases the performance difference between the two options is so small that it doesn't have any effect on the user experience.
As a developer you will only spend your time (and make the code less maintainable) if you try to improve the program performance with such tweaks.

2. It is also difficult to correctly measure the effects of these micro-optimizations.

Specifically in this case, based on my tests, there is ZERO difference between the two options.

I tested it with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim b As Boolean = False
   Dim m As Int = 10000000
   Dim n As Long = DateTime.Now
   For i = 1 To m
     If b = False Then
       
     End If
   Next
   Log(DateTime.Now - n) '76
   Dim n As Long = DateTime.Now
   For i = 1 To m
     If Not(b) Then
       
     End If
   Next
   Log(DateTime.Now - n) '42
   n = DateTime.Now
   For i = 1 To m
     If b = False Then
       
     End If
   Next
   Log(DateTime.Now - n) '43
   Dim n As Long = DateTime.Now
   For i = 1 To m
     If Not(b) Then
       
     End If
   Next
   Log(DateTime.Now - n) '43
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I find posts like this to be more harmful than useful.
Thank you for your compliments :D


As a developer you will only spend your time (and make the code less maintainable) if you try to improve the program performance with such tweaks.
I agree that maintainability is very important; in this specific case, use the second way does not affect anything.


My test:
B4X:
' Activity_Create

    Dim now As Long = DateTime.Now
    For i = 0 To 100000
        routine
    Next
    now = DateTime.Now - now
    DateTime.TimeFormat = "ss:SSS"
    Log(DateTime.Time(now))


Sub routine
    Dim A As Boolean = True
    If a = False Then
        Dim b As Int
    End If
End Sub


If A = False: 10ms
If Not(A): 17ms


However, you (Erel) use always "If condition = False" in your (example) codes :)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Regarding micro-optimizations in general, I believe it really depends on the project. For example, when querying a database, no one really cares if it takes 1.2 or 1.8 seconds to complete. In a physics simulation, however every millisecond matters. It can represent the difference between having 1000 or even 1000000 more particles on screen.

The reason we discuss these things is because we CARE and LOVE B4x. :)
Luca and I have been here for years and I believe we only want to see it thrive.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't see any difference when I test it with your code. The results are exactly the same. (not that it is really important)
The one that will be called first will be a bit slower on the first iteration. But other than that the performance is identical.
B4X:
Sub Activity_Create(FirstTime As Boolean)
   For t = 1 To 10
        Dim now As Long = DateTime.Now
    For i = 0 To 100000
    routine1
    Next
    now = DateTime.Now - now
    DateTime.TimeFormat = "ss:SSS"
    Log("A = False: " & DateTime.Time(now))
      Dim now As Long = DateTime.Now
    For i = 0 To 100000
    routine2
    Next
    now = DateTime.Now - now
    DateTime.TimeFormat = "ss:SSS"
    Log("Not(A): " & DateTime.Time(now))
   Next
End Sub


Sub routine1
  Dim A As Boolean = True
  If a = False Then
  Dim b As Int
  End If
End Sub

Sub routine2
  Dim A As Boolean = True
  If Not(A) Then
  Dim b As Int
  End If
End Sub

e reason we discuss these things is because we CARE and LOVE B4x. :)
Luca and I have been here for years and I believe we only want to see it thrive.
I didn't think otherwise. Really.
 
Upvote 0
Top