Tip for concatenating large numbers of strings

Monkey Forums/Monkey Code/Tip for concatenating large numbers of strings

Peeling(Posted 2015) [#1]
If you find yourself needing to concatenate a lot of strings (for me, it was exporting image usage information for our proprietary UI module), it is considerably faster to do so as a series of pairings than by repeatedly adding new strings to the end of an ever-growing output string:



Initially the image usage information step was taking over a minute to run, and when I added some logging to find out why, I discovered that it was almost entirely down to string concatenation. Changing the method to the one above eliminated all but half a second of the processing, and made the concatenation virtually instantaneous.


ImmutableOctet(SKNG)(Posted 2015) [#2]
Depending on what you're doing, you could just use a 'Stream' (Or 'DataStream'). Like the 'List' approach, that's a lot better than constantly allocating and moving over the contents every time you add to it. And, with a 'Stream', the garbage collector can take care of your left over 'Strings' as you go along.


Nobuyuki(Posted 2015) [#3]
Perhaps it would be faster (if many strings need to be concatenated) to blast the chars to a data buffer, then use String.FromChars at the end to convert it back? It would be nice if Monkey had a string builder class...


ziggy(Posted 2015) [#4]
StringStack and Join method can do this very efficiently, and get rid of string concatenation


skid(Posted 2015) [#5]
I typically use the following:


local concat$="".Join(stringlist.ToArray())