Thanks Alwyn - for a better test, please run it with #pragma disableBackgroundTasks as this will remove the yield check at the loop boundary, and give a better estimate of the time spent in the loop.
#pragma disableBackgroundTasks
Dim i as Integer
Dim result as Boolean
Dim dRoot As Double
Dim rnd As new Random
dRoot = rnd.InRange(1, 100) / 10
for i = 1 to 10000000
result = (dRoot = floor(dRoot))
next i
TOTAL TIME = 1054 milliseconds (12.96% speed improvement)
#pragma disableBackgroundTasks
Dim i as Integer
Dim result as Boolean
Dim dRoot As Double
Dim rnd As new Random
dRoot = rnd.InRange(1, 100) / 10
for i = 1 to 10000000
result = ((dRoot \\ 1) = dRoot)
next i
TOTAL TIME = 829 milliseconds (11.71% speed improvement)
Hmmm, didn’t realize “disableBackgroundTasks” would make such a big speed difference. Thanks for the tip Michael.
[quote=22815:@Alwyn Bester]
Hmmm, didn’t realize “disableBackgroundTasks” would make such a big speed difference. Thanks for the tip Michael.[/quote]
By default, Xojo checks to see whether it need to yield to other threads on every loop iteration - that’s a non-trivial amount of code, and as you’ve demonstrated, if the code inside the loop is tiny, there is move time spent in the loop housekeeping than in the code inside the loop.
My practice: I tend to put #pragma disableBackgroundTasks everywhere, and then as I fine-tune the app for performance I’ll add explicit Yields() in strategic places.
I believe that #pragma disableBackgroundTasks only affects code that comes after it in the method, which is different than other pragmas such as #stackChecking which I think affects the entire method regardless of where it shows up (I may be wrong on this, don’t quote me)
i just try putting #pragma disableBackgroundTask in the beginning of method that i have for next loop (eg looping through all the controls in a form). Seem the application a little faster.
in what situation i can make use of this pragma #pragma StackChecking ?
Pragmas are “expert” features, so you should definitely read about them in the Language Reference and be cautious using them.
In my experience, the one that makes the most difference in performance is DisableBackgroundTasks. If you absolutely need every single bit of performance, you might also try DisableBoundsChecking, NilObjectChecking and StackOverflowChecking but these are dangerous as they turn off important error checking at runtime. Only use those with perfectly debugged code.