LucaMs Expert Licensed User Longtime User Nov 16, 2016 #1 Usually I use code like this: Code: Copy If Not(Condition) Then instead of: Code: Copy If Condition = False Then because I think the first line is more readable. Well, I did a simple test and it seems to me that the first way is much slower (probably about 80%) This is just a... "warning"
Usually I use code like this: Code: Copy If Not(Condition) Then instead of: Code: Copy If Condition = False Then because I think the first line is more readable. Well, I did a simple test and it seems to me that the first way is much slower (probably about 80%) This is just a... "warning"
wonder Expert Licensed User Longtime User Nov 16, 2016 #2 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
This has been know for a while, but thanks for bringing it up... For mass replacement, press F3, and Replace "Not(" with "False = (".
Erel B4X founder Staff member Licensed User Longtime User Nov 16, 2016 #3 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: Code: Copy 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
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: Code: Copy 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
LucaMs Expert Licensed User Longtime User Nov 16, 2016 #4 Erel said: I find posts like this to be more harmful than useful. Click to expand... Thank you for your compliments Erel said: 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. Click to expand... I agree that maintainability is very important; in this specific case, use the second way does not affect anything. My test: Code: Copy ' 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
Erel said: I find posts like this to be more harmful than useful. Click to expand... Thank you for your compliments Erel said: 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. Click to expand... I agree that maintainability is very important; in this specific case, use the second way does not affect anything. My test: Code: Copy ' 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
wonder Expert Licensed User Longtime User Nov 16, 2016 #5 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
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.
Erel B4X founder Staff member Licensed User Longtime User Nov 16, 2016 #6 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. Code: Copy 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 wonder said: 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. Click to expand... I didn't think otherwise. Really. Upvote 0
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. Code: Copy 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 wonder said: 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. Click to expand... I didn't think otherwise. Really.
LucaMs Expert Licensed User Longtime User Nov 17, 2016 #7 Erel said: I don't see any difference when I test it with your code. Click to expand... I used the code in my post #4, changing and running again the app and the result were those (10ms vs 17ms); this way I had to write less code Erel said: I didn't think otherwise. Really. Click to expand... I know Upvote 0
Erel said: I don't see any difference when I test it with your code. Click to expand... I used the code in my post #4, changing and running again the app and the result were those (10ms vs 17ms); this way I had to write less code Erel said: I didn't think otherwise. Really. Click to expand... I know