title: Memory-Mapped File is Faster on Huge Sequential Read? Why?
body: <p>I used the code below to measure the performance difference between reading <em>large, sequential</em> reads of a memory-mapped file, as compared to just calling <code>ReadFile</code>:</p>
  
  <pre><code>HANDLE hFile = CreateFile(_T("D:\\LARGE_ENOUGH_FILE"),
      FILE_READ_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
      FILE_FLAG_NO_BUFFERING, NULL);
  __try
  {
      const size_t TO_READ = 32 * 1024 * 1024;
      char sum = 0;
  #if TEST_READ_FILE
      DWORD start = GetTickCount();
      char* p = (char*)malloc(TO_READ);
      DWORD nw;
      ReadFile(hFile, p, TO_READ, &amp;nw, NULL);
  #else
      HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY,
          0, 0, NULL);
      const char* const p = (const char*)MapViewOfFile(hMapping,
          FILE_MAP_READ, 0, 0, 0);
      DWORD start = GetTickCount();
  #endif
      for (size_t i = 0; i &lt; TO_READ; i++)
      {
          sum += p[i]; // Do something kind of trivial...
      }
      DWORD end = GetTickCount();
      _tprintf(_T("Elapsed: %u"), end - start);
  }
  __finally { CloseHandle(hFile); }
  </code></pre>
  
  <p>(I just changed the value of <code>TEST_READ_FILE</code> to change the test.)</p>
  
  <p>To my surprise, <code>ReadFile</code> was <em>slower</em> by ~20%! Why?</p>