Yes, string concatenation is basically slow, since it usually involves making a new copy of the whole buffer with some extra bit appended.

The fastest way to do this is to use buffered IO. How do you write your file currently ? Depending on the extent of the speed problem, you may want to consider implementing your own buffering method, but that could be an overkill.