
You want to determine if there is a performance advantage to using the MemoryMappedFile class in System.IO.MemoryMappedFiles with the C# language. This type provides an abstract data type that puts a file into memory, but allows for better memory management than using arrays in some cases.
MemoryMappedFile: Can be used to improve performance of binary file loading. In this test, it was faster than FileStream in all executions.

First, this program loads a binary file of 4.37 MB that contains binary data; the file is essentially a grouping of about 1400 smaller files. To do this, the program uses FileStream and then BinaryReader; MemoryMappedFile, MemoryMappedStream, and then BinaryReader; and finally File.ReadAllBytes, MemoryStream and BinaryReader.
This C# example program benchmarks the MemoryMappedFile type.
Program that benchmarks MemoryMappedFile [C#]
using System;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
class Program
{
static void Main()
{
const int max = 1;
var s1 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
Test1();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
Test2();
}
s2.Stop();
var s3 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
Test3();
}
s3.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
max).ToString("0.00 ns"));
Console.WriteLine(((double)(s3.Elapsed.TotalMilliseconds * 1000 * 1000) /
max).ToString("0.00 ns"));
}
static void Test1()
{
// FileStream.
using (FileStream file = File.Open("C:\\P.bin", FileMode.Open))
{
Read(file);
}
}
static void Test2()
{
// MemoryMappedFile.
using (MemoryMappedFile file = MemoryMappedFile.CreateFromFile("C:\\P.bin"))
using (MemoryMappedViewStream stream = file.CreateViewStream())
{
Read(stream);
}
}
static void Test3()
{
// MemoryStream.
using (MemoryStream stream = new MemoryStream(File.ReadAllBytes("C:\\P.bin")))
{
Read(stream);
}
}
static void Read(Stream stream)
{
// This method reads in the file-format specific values.
using (BinaryReader reader = new BinaryReader(stream))
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string u = reader.ReadString();
int len = reader.ReadInt32();
byte[] b = reader.ReadBytes(len);
}
}
}
}
Results when max = 1
9671400.00 ns
6737300.00 ns
6958400.00 ns
Results when max = 100
7246513.00 ns
4726050.00 ns
7294708.00 nsResults. The results of this benchmark are interesting and show that MemoryMappedFile is very fast. For loading the file only once, MemoryMappedFile is faster than the other two approaches; the FileStream approach is the slowest. For loading the file 100 times, MemoryMappedFile is somewhat less than twice as fast as the other approaches.
File.Open Examples
Unfortunately, the program requires a specific binary file that I am not able to share with you. However, if you want to reproduce the benchmark, you could use any large file and simply read all the bytes in sequentially using BinaryReader.

MemoryMappedFile provides a way to load a file with very good performance; it seems to have better performance than FileStream and also the File.ReadAllBytes approach. For this reason, MemoryMappedFile, which is only available in .NET 4.0 or later, is ideal for loading binary files in this way.
File Handling