I'm using the .Net micro framework so the StringBuilder is not available.
I have seen some code from apt professionals to use an Arraylist of chars to concat and build strings, as opposed to the + operator. They essentially build a managed code StringBuilder.
Is there a performance advantage to this? Assume the number of concatenations are higher than 10 and string lengths are also larger than 10.
No, don't use an ArrayList
of char
values. That will box every char - performance will be horrible, as will memory usage. (Size of a reference + size of a boxed char for each character... yikes!)
Use a char[]
internally and "resize" it (create a new array and copy the contents in) when you need to, perhaps doubling in size each time. (EDIT: You don't resize it to the exact size you need - you would start off with, say, 16 chars and keep doubling - so most Append
operations don't need to "resize" the array.)
That's similar to how StringBuilder
works anyway. (It's even closer to how Java's StringBuilder
works.)
I suggest you actually build your own StringBuilder
type with the most important members. Unit test the heck out of it, and profile where appropriate.
Let me know if you want a short example.