Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simplistic misunderstanding about ; + "" in a print
#3
Actually, contrary to what you guys might think and how logic would normally dictate things, it's actually better to use + (Concatenation) than it is to use ; (semicolons) in QB64.  I know that sounds counter intuitive, but I swear, it's true.  Tongue

You have to remember, QB64 is a translator which turns BAS programs into C programs, and ... that's not always pretty.  Let me draw you a picture in code:

Code: (Select All)
a$ = "foo"
b$ = "bar"
c$ = "but"

Print a$; b$; c$
Print a$ + b$ + c$

Now, let's break this down into three sections off the translated code which we can find in internal/temp:

Code: (Select All)
do{
qbs_set(__STRING_A,qbs_new_txt_len("foo",3));
qbs_cleanup(qbs_tmp_base,0);
if(!qbevent)break;evnt(1);}while(r);
do{
qbs_set(__STRING_B,qbs_new_txt_len("bar",3));
qbs_cleanup(qbs_tmp_base,0);
if(!qbevent)break;evnt(2);}while(r);
do{
qbs_set(__STRING_C,qbs_new_txt_len("but",3));
qbs_cleanup(qbs_tmp_base,0);
if(!qbevent)break;evnt(3);}while(r);

The above is basically where we translate the variables into c-code, so those represent the first few lines of the QB64 program.

Code: (Select All)
do{
tqbs=qbs_new(0,0);
qbs_set(tqbs,__STRING_A);
if (is_error_pending()) goto skip1;
makefit(tqbs);
qbs_print(tqbs,0);
qbs_set(tqbs,__STRING_B);
if (is_error_pending()) goto skip1;
makefit(tqbs);
qbs_print(tqbs,0);
qbs_set(tqbs,__STRING_C);
if (is_error_pending()) goto skip1;
makefit(tqbs);
qbs_print(tqbs,0);
qbs_print(nothingstring,1);
skip1:
qbs_free(tqbs);
qbs_cleanup(qbs_tmp_base,0);
if(!qbevent)break;evnt(5);}while(r);

And all the above is basically how we print a$; b$; c$.  That's ONE line of QB64 code translated into all of those.

Code: (Select All)
do{
tqbs=qbs_new(0,0);
qbs_set(tqbs,qbs_add(qbs_add(__STRING_A,__STRING_B),__STRING_C));
if (is_error_pending()) goto skip2;
makefit(tqbs);
qbs_print(tqbs,0);
qbs_print(nothingstring,1);
skip2:
qbs_free(tqbs);
qbs_cleanup(qbs_tmp_base,0);
if(!qbevent)break;evnt(6);}while(r);

And, the above is basically how we translate that last line of BAS code.

It's a little hard to understand that we're seeing with the above, but what it basically breaks down to is:

PRINT a$; b$; c$;    <---- This statement will get translated as if it was actually:
PRINT a$;
PRINT b$;
PRINT c$;

Each of those get printed first to a temp string (for formatting and word wrap and all that), and as you can see from the above, that's a good number of lines.


The second statement (PRINT a$ + b$ + c$) basically gets broke down to:
Print ((a$ + b$) + c$)

One print statement.  One use of a temp string,  One call to the formatting routine.  



The more semicolons you have, the bigger the difference becomes.  

Most would think the PRINT with semicolons would be better.  In most cases, it probably would be.  It *should* be.   But, due to the way we machine translate things and do our formatting and word wrap and everything else, it's NOT. 


Just something to keep in mind, that most folks never consider.  It's behind the scene translation at work on your code.
Reply


Messages In This Thread
RE: simplistic misunderstanding about ; + "" in a print - by SMcNeill - 09-19-2024, 02:13 AM



Users browsing this thread: 2 Guest(s)