One thing to note when it comes to performance -- most things you write in QB64 are going to always be slower than any counterpart in C or what not. Our internal commands are usually based on C, and are pretty quick, but once you start writing the equivalent in BASIC, they slow down somewhat.
The reason?
Each command does error checking and system checking.
You might just write a program that counts to 10..
FOR i = 1 to 10
NEXT
This simple little program gets translated into this mess:
Code: (Select All)
fornext_value2= 1 ;
fornext_finalvalue2= 10 ;
fornext_step2= 1 ;
if (fornext_step2<0) fornext_step_negative2=1; else fornext_step_negative2=0;
if (is_error_pending()) goto fornext_error2;
goto fornext_entrylabel2;
while(1){
fornext_value2=fornext_step2+(*__SINGLE_I);
fornext_entrylabel2:
*__SINGLE_I=fornext_value2;
if (fornext_step_negative2){
if (fornext_value2<fornext_finalvalue2) break;
}else{
if (fornext_value2>fornext_finalvalue2) break;
}
fornext_error2:;
if(qbevent){evnt(1);if(r)goto S_1;}
fornext_continue_1:;
}
fornext_exit_1:;
Notice how much error checking is in there? and event checking? This is so that things such as clicking the little red X on the top right of the window will close the program between process and all. It's so you don't go out of bounds and other issues...
In C, you could write that code without all that error checking and event checking, but it's not something which QB64 can afford to pass up on. Now, these are *small* time sinks, but they add up.
IF everyone wrote perfect, error free code, these checks wouldn't be necessary. Since Steve, however, codes in QB64PE, I'm afraid those are necessary so I don't melt down my computer or destroy something, as *I* certainly don't write perfect, error free code.
So *why* is INSTR faster than a self-created routine?
It's been optimized directly for the underlying language that we translate BAS code into and skips a lot of these type checks that are necessary at each stage when you try and write the same routine yourself. Unless you want to write C yourself and skip those BAS checks, I don't think you'd ever be able to write something that would be faster than what's packaged under the hood.