Tip for concatenating large numbers of strings
Monkey Forums/Monkey Code/Tip for concatenating large numbers of strings
| ||
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. |
| ||
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. |
| ||
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... |
| ||
StringStack and Join method can do this very efficiently, and get rid of string concatenation |
| ||
I typically use the following:local concat$="".Join(stringlist.ToArray()) |