views:

475

answers:

3

For a situation like capturing text incrementally, for example if you were receiving all of the output.write calls when a page was rendering, and those were being appended into a textwriter over a stringbuilder.

Is there a more efficient way to do this? Something that exists in dotnet already preferably? Especially if there's a total size over a hundred k. Maybe something more like an array of pages rather than contiguous memory?

+2  A: 

I think StringBuilder is the most efficient way to append text in .net. To be more efficient you can specify the initial size of the StringBuilder when you create it.

sh_kamalh
A: 

That's as good as it gets. You can use a StringWriter but it's still writing into a StringBuilder

Wolfwyrd
+2  A: 

It depends on what you're doing with that text.

If the concern is tracing or logging, I'd say your best bet is to use ETW (Event Tracing for Windows). It's a kernel-level tracing facility that's been built into Windows since Windows 2000 and it's much, much faster than doing file I/O.

If you're not using .NET 2.0, you have to do a little win32 API work to use it, and you have to create a provider class that you register on the system. It's a little complicated but worth the effort.

If you're using .NET 3.5, the managed Etw classes can be found in System.Diagnostics.Eventing.

jonsequitur