Why is C ++ fseek / fread performance more than C # FileStream Seek / Read

I am doing a fairly simple test:

  • You have a large ~ 6Gb random binary file
  • The algorithm makes a repeat loop "SeekCount"
  • Each repetition does the following:
    • Calculates random offset within file size
    • Looking for this offset
    • Reads a small data block

WITH#

    public static void Test()
    {
        string fileName = @"c:\Test\big_data.dat";
        int NumberOfSeeks = 1000;
        int MaxNumberOfBytes = 1;
        long fileLength = new FileInfo(fileName).Length;
        FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, FileOptions.RandomAccess);
        Console.WriteLine("Processing file \"{0}\"", fileName);
        Random random = new Random();
        DateTime start = DateTime.Now;
        byte[] byteArray = new byte[MaxNumberOfBytes];

        for (int index = 0; index < NumberOfSeeks; ++index)
        {
            long offset = (long)(random.NextDouble() * (fileLength - MaxNumberOfBytes - 2));
            stream.Seek(offset, SeekOrigin.Begin);
            stream.Read(byteArray, 0, MaxNumberOfBytes);
        }

        Console.WriteLine(
            "Total processing time time {0} ms, speed {1} seeks/sec\r\n",
            DateTime.Now.Subtract(start).TotalMilliseconds, NumberOfSeeks / (DateTime.Now.Subtract(start).TotalMilliseconds / 1000.0));

        stream.Close();
    }

Then do the same test in C ++:

void test()
{
     FILE* file = fopen("c:\\Test\\big_data.dat", "rb");

char buf = 0;
__int64 fileSize = 6216672671;//ftell(file);
__int64 pos;

DWORD dwStart = GetTickCount();
for (int i = 0; i < kTimes; ++i)
{
    pos = (rand() % 100) * 0.01 * fileSize;
    _fseeki64(file, pos, SEEK_SET);
    fread((void*)&buf, 1 , 1,file);
}
DWORD dwEnd = GetTickCount() - dwStart;
printf(" - Raw Reading: %d times reading took %d ticks, e.g %d sec. Speed: %d items/sec\n", kTimes, dwEnd, dwEnd / CLOCKS_PER_SEC, kTimes / (dwEnd / CLOCKS_PER_SEC));
fclose(file);
}

Lead time:

  • C #: 100-200 read / sec
  • C ++: 250,000 reads / sec (250 thousand)

Question: why is C ++ a thousand times faster than C # with such a trivial operation as reading a file?

Additional Information:

  • I played with stream buffers and set them to the same size (4Kb)
  • Disk defragmented (0% fragmentation)
  • : Windows 7, NTFS, - 500 (WD, ), 8 ( ), 4 Core CPU ( )
+5
1

++ : , , , ++ .

@MooingDuck:

Rand()/ (RAND_MAX) * FileSize

++, # - 200 /.

.

+6

All Articles