Author Topic: String Concatenate  (Read 1233 times)

ScriptBasic

  • Guest
String Concatenate
« on: May 01, 2018, 04:20:51 AM »
I notice a thread (argument) on the PowerBASIC forum about the best way to concatenate a string. I thought I would extend that thread here and get your opinion/advice on concatenating a string. The only rule is the string must be populated with nulls.  (CHR$(0)) Keep in mind that the string you create has to be deallocated (freed) before returning to the command prompt.

Here is the Script BASIC attempt for reference.

Code: [Select]
FOR idx = 1 TO 1000
  this &= 0x0
NEXT
PRINT LEN(this),"\n"


jrs@jrs-laptop:~/sb/examples/test$ time scriba concat.sb
1000

real   0m0.006s
user   0m0.004s
sys   0m0.000s


100000

real   0m0.988s
user   0m0.948s
sys   0m0.036s


This example just allocates a 1,000,000 byte string of nulls.

Code: [Select]
this = STRING(1000000, 0)
PRINT LEN(this),"\n"


jrs@jrs-laptop:~/sb/examples/test$ time scriba 1milstr.sb
1000000

real   0m0.009s
user   0m0.004s
sys   0m0.000s
jrs@jrs-laptop:~/sb/examples/test$
« Last Edit: May 01, 2018, 05:07:47 AM by John »

Mike Lobanovsky

  • Guest
Re: String Concatenate
« Reply #1 on: May 01, 2018, 04:19:54 PM »
In a mature pointer aware BASIC dialect like PB, doing string concatenation in a loop would be a really bad naive idea. Use a large preallocated string/byte buffer and fill it with content keeping track of chunk "head" and "tail" pointers, and redim preserve/realloc the buffer to double its size just once or twice if it appears too small for whatever reason. Avoid excessive time-costly memory reallocation inherent in "atomic" string concatenation.

Alternatively, use PB's stock "string builder" that implements the above technique OOTB.

Still better yet, ask Hutch (or JJ) how to re-implement it in raw inline assembler to fit your immediate needs to achieve the maximum speed possible.

Dartmouth BASIC partisans will however have to resort to String$/Mid$ in a hope that their particular dialect authors were smart enough to implement the in-place Mid$ statement without the undue memory reallocation step where possible.
« Last Edit: May 01, 2018, 04:40:15 PM by Mike Lobanovsky »