Question: Is StringTools['StringBuffer'] outdated now?

Since strings are not mutable objects in Maple, the StringTools package provides two procedures, StringTools:-OldStringBuffer and StringTools:-StringBuffer, which appear heavily correlated with Java's StringBuffer and StringBuilder

The help page of StringBuffer claims that use of a StringBuffer is much more efficient than the naive approach: 

(*
`G` and `F` are taken from the link above.
*)
G := proc()
   description "extremely inefficient string concatenator";
   local   r;
   r := proc()
       if nargs = 0 then
           ""
       elif nargs = 1 then
           args[ 1 ]
       else
           cat( args[ 1 ], procname( args[ 2 .. -1 ] ) )
       end if
   end proc;
   r( args )
end proc:
# # This can be transformed into an O(1) algorithm by passing a string buffer to the recursive calls.
F := proc()
   description "efficient version of G";
   local    b, r;
   b := StringTools:-StringBuffer();
   r := proc()
       if nargs = 1 then
           b:-append( args[ 1 ] )
       else
           b:-append( args[ 1 ] );
           procname( args[ 2 .. -1 ] )
       end if
   end proc;
   r( args ):-value()
end proc:
s := 'StringTools:-Random(10, print)' $ 1e4:
NULL;
time(G(s));
                             5.375

time(F(s));
                             1.125

But why not use the built-in cat directly? 

time(cat(s));
                               0.

time(StringTools:-Join([s], ""));
                               0.

Clearly, this is even more efficient

Here is the last example in that link. 

FilterFile := proc( fname::string, filter )
   local   b, line;
   b := StringTools:-StringBuffer();
   do
       line := readline( fname );
       if line = 0 then break end if;
       b:-append( filter( line ) )
   end do;
   b:-value()
end proc: # verbatim 
filename__0 := FileTools:-JoinPath(["example", "odyssey.txt"], 'base' = 'datadir'):
filename__1 := URL:-Download("https://gutenberg.org/ebooks\
/2600.txt.utf-8", "War-and-Peace.txt"):

fclose(filename__0):
    time[real]((rawRes0 := FilterFile(filename__0, StringTools:-Unique)));
                             0.223

fclose(filename__1):
    time[real]((rawRes1 := FilterFile(filename__1, StringTools:-Unique)));
                             1.097

Nevertheless, 

close(filename__0):
use StringTools, FileTools:-Text in
	time[real]((newRes0 := String(Support~(fscanf(filename__0, Repeat("%[^\n]%*c", CountLines(filename__0))))[])))
end;
                             0.118

close(filename__1):
use StringTools, FileTools:-Text in
	time[real]((newRes1 := String(Support~(fscanf(filename__1, Repeat("%[^\n]%*c", CountLines(filename__1))))[])))
end;
                             0.580

evalb(newRes0 = rawRes0 and newRes1 = rawRes1);
                              true

As you can see, these experiments just tell an opposite story. Isn't the so-called "StringBuffer" obsolete today

Please Wait...