C# Text Processing – How to Load Text Files into Memory-Mapped Files

ctext processing

I have a number of large text files that I need to manipulate in a highly performant manner. I've decided to look into using Memory Mapped files in C# (.NET 4). However, I can't find any examples of or guidance on how to read/write to a memory mapped file that, underneath, is bound to a text file. Can anyone shed some light on this and, possibly, provide me with some examples?

If it helps, I’ll be reading text in whole lines, then altering the line and replacing it with content that may be shorter or longer than the original. After all of the manipulations are done, I want to save it back to disk as a text file.

Best Answer

I do not think you will gain much, if anything, in performance, by using memory-mapped files instead of performing normal text-file processing. From the moment that you change the length of a single line even by just one byte, the remainder of the file will need to be read, shifted by one byte, and written back to disk. From the point of view of I/O, this is equivalent to normal text-file processing: Read a line, modify it, write it, repeat. And the headache of having to do by yourself all the text processing is probably not worth the hassle.

Have you established an acceptable performance metric for your system?

Have you tried the normal text file processing approach and found it to exceed that metric before starting to look for a more efficient solution?

Related Topic