[Haskell-cafe] Efficient string construction

Daniel Fischer daniel.is.fischer at web.de
Thu Jun 3 10:39:34 EDT 2010

On Thursday 03 June 2010 16:03:11, Kevin Jardine wrote:
> (I've done a basic Google search on this with no results. Apologies if
> this has been asked before.)
> I am coding a web application in which the content is a Unicode string
> built up over multiple functions and maintained in a State structure.
> I gather that the String module is inefficient and that Data.Text would
> be a better choice.
> Is it more efficient to build up a list of Text objects over time and
> combine them together with a single Data.Text.concat for the final
> output or to run Data.Text.append for each new string so that I am
> maintaining a single Text object rather than a list?
> As Data.Text.append requires copying both strings each time, my gut
> feeling is that concat would be much more efficient, but Haskell has
> surprised me before, so I wanted to check.
> Kevin

I'd say, use Data.Text.Lazy and its 'fromChunks' function if you produce 
the string chunkwise. That avoids copying.
Perhaps Data.ByteString[.Lazy].UTF8 is an even better choice than Data.Text 
(depends on what you do).

More information about the Haskell-Cafe mailing list