I am searching for a way to read text file content very fast, for example: 2GB file in under 20 seconds (without loading the entire file into memory, thus keeping the memory usage low).
I have read here about Memory Mapped Files. The CreateFromFile
method can create a file mapping in memory. I have tested this code:
using(var mmf1 = MemoryMappedFile.CreateFromFile(file, FileMode.Open)) {
using(var reader = mmf1.CreateViewAccessor()) {
//...
}
}
My question is: is this the correct way to index the file into memory so that it will be available for fast search?
Once the file has been mapped/indexed into memory, I hope to use a Parallel
loop to search the file for content. Right now I am testing this implementation. If I understand it correctly, it uses 1 thread to read a line in a file, and the 2nd thread to check/process the line.
Is it possible to use 1 thread to read and process the file content from top to bottom and the 2nd thread to read file content from the bottom up, thus increasing the search speed by a factor of 2.
Because as soon as the needed content has been found, all threads must stop search.