05-04-2025, 09:24 PM
(05-04-2025, 01:09 PM)mdijkens Wrote: As a last step, when you are fully confident about your code, use $checking:off for huge loops
One important thing to note here -- I don't recommend this unless you're careful and certain of what you're doing. The issue isn't even writing error free code; the issue is how QB64 handles its internal events. You could have something simple like this that works 100% fine:
Code: (Select All)
$Checking:Off
Do
_Limit 30
i = _LoadImage("blah blah.jpg", 32)
t = _CopyImage(i)
_FreeImage i
_FreeImage t
Loop Until _KeyHit
Now, give that a run and see what happens. Click the X in the top right corner to close it. Hit any key to end it. Try to stop it as you wish...
...and then go into your task manager and terminate it manually because it's stuck.
That's 100% error free code, but it relies on an external file. A file that isn't included in the download. It can't report that to you, and it can't get past the glitch in the code, so it just... locks up in an endless loop of trying to report something it can't.
$CHECKING:OFF can help speed up your programs as it eliminates a lot of the QB64 internal checks, but it can also lead to serious issues. Lose a network connection? Dismount a drive? File not found? User inputs a 0 which would generate a division by 0 error? Any and all errors that you might normally catch and be safe against, can now potentially lock you up and utterly break your program.
For many things, the slight increase in speed isn't really worth it.
Where IS it worth it??
Around code tested _MEM blocks. Around various arrays. Both of those have been altered to maximize performance once checking is off. (Ask @RhoSigma for more details about arrays and checking off, as he was the one who fixed those with us and I don't remember all the details for them off the top of my head now.)
Any other place where I'd use Checking:Off? Small tight loops that are going to do one thing, do it repeatedly, and where I'm convinced that I can't generate an error. (Around a sort routine, or a shuffle routine, for example, as once written those tend to be fairly error proof.)
Otherwise, I'd be cautious about overuse of $Checking:Off, particularly around large blocks of code. Those safeguards are there for a reason. Is it really worth a tenth of a second in processing times to skip them?
Let's compare what we're trimming out with that $Checking:Off above:
Code: (Select All)
S_1:;
do{
if(qbevent){evnt(1);if(r)goto S_1;}
do{
sub__limit( 30 );
if(!qbevent)break;evnt(2);}while(r);
do{
*__SINGLE_I=func__loadimage(qbs_new_txt_len("blah blah.jpg",13), 32 ,NULL,0|1);
qbs_cleanup(qbs_tmp_base,0);
if(!qbevent)break;evnt(3);}while(r);
do{
*__SINGLE_T=func__copyimage(qbr(*__SINGLE_I),NULL,0);
if(!qbevent)break;evnt(4);}while(r);
do{
sub__freeimage(qbr(*__SINGLE_I),1);
if(!qbevent)break;evnt(5);}while(r);
do{
sub__freeimage(qbr(*__SINGLE_T),1);
if(!qbevent)break;evnt(6);}while(r);
S_7:;
dl_continue_1:;
}while((!(func__keyhit()))&&(!is_error_pending()));
dl_exit_1:;
if(qbevent){evnt(7);if(r)goto S_7;}
sub_end();
return;
And with $Checking:OFF?
Code: (Select All)
do{
sub__limit( 30 );
*__SINGLE_I=func__loadimage(qbs_new_txt_len("blah blah.jpg",13), 32 ,NULL,0|1);
qbs_cleanup(qbs_tmp_base,0);
*__SINGLE_T=func__copyimage(qbr(*__SINGLE_I),NULL,0);
sub__freeimage(qbr(*__SINGLE_I),1);
sub__freeimage(qbr(*__SINGLE_T),1);
dl_continue_1:;
}while((!(func__keyhit()))&&(!is_error_pending()));
dl_exit_1:;
sub_end();
return;
}
That looks like an impressive amount of code all cleaned up, but what are those lines that we stripped out? Basically they're all this:
do{
sub__limit( 30 );
if(!qbevent)break;evnt(2);}while(r);
And that breaks down to basically becoming:
DO
Try to implement the _LIMIT 30 command
LOOP until there's no internal error at this stage.
So if there's an error, we report it. Otherwise it's basically a simple IF check that we bypass quickly and quietly.
In total, we skipped all of 7 simple IF checks... How long do they take to process in a cycle? Can you even measure the hit to performance? The speed you're saving here is negligible, but the trouble you're opening yourself up for could be rather badly unwanted.
Save $Checking:Off for the places where it can make a true difference in performance without having to worry over something unexpected destroying your program. Small, tight blocks with mem and arrays is the place to remember it. Other places.... just be very cautious and think twice if it's worth the inherent risks for trouble in your code.