Since I encountered a performance problem with a complex application I programmed in Thunkable (especially on older phones), I decided to explore ways to speed up the application by optimizing the code for speed (sometimes at the cost of degrading its readability).
Here is the first part of my research, how to speed up Thunkable apps and optimize code for speed.
PART 1.
I use the blocks shown below for my tests. The content under test is inserted in the middle, between Timer1_Start and Timer1_Stop.
First I checked how much time a million iterations of the empty loop takes, to make sure there is no significant impact on the result.
It is (on my phone) 25 ms. Since I use far fewer iterations with these tests, the error is negligible.
- APP VARIABLE vs STORED VARIABLE, 100x
App variable (2 ms) is tremendously faster than stored variable (402 ms).
Conclusion: avoid using stored variable in deeply nested, repeatedly executed block fragments. Use app variable instead, and only store the final result (if you need it) in stored variable.
- CHANGE VARIABLE BY +1 vs SET VARIABLE TO VARIABLE +1, 1000x
Surprise! CHANGE BY +1 gets 27 ms, while SET V to VARIABLE +1 is only 8 ms (!!!). That’s more than three times faster. All the places in my code where I used CHANGE BY x, I converted to old-fashioned V = V + x.
- FUNCTION vs DIRECT BLOC, 10000x
Another surprise. With 10000 iterations only 45 ms was enough to assign empty text to a variable. However, in function it takes an eternity, more than 120000 ms (hm, two minutes…).
Conclusion: avoid using functions in deeply nested, repeatedly executed program fragments. Use direct błocka instead. Use functions as a grouping of bigger blocks of code, not small, few block operations, that are executed often.
For balance , let’s also look at examples of three solutions that provide the same processing speed:
-
REPEAT vs FOR
-
STACKED IF/ELSE vs IF/ELSE IF
-
Loading (as background, element background or image) PNG, WEBL, JPG (with the same resolution and color depth). It impacts apk size only.
More tricks to speed up Thunkable code and accelerate applications in the next part (PART2), soon.
Greetings for all Thunkers