title: To read a file,which one will be faster?using fstream,FILE* or memory mapping?
body: <p>Here is my project configuration：vs2013,win32,Debug.  </p>
  
  <p>I want to know for different file sizes,which of the 3 file reading methods will be faster.They are c++ style fstream,c style file read and write and memory mapping.  </p>
  
  <p>But after execution,here is my result:  </p>
  
  <blockquote>
    <p>File Size 1225284<br>
    fstream time 47<br>
    c file pointer time 0<br>
    memory mapping time 0  </p>
    
    <p>File Size 14856192<br>
    fstream time 15<br>
    c file pointer time 0<br>
    memory mapping time 47</p>
    
    <p>File Size 97198080<br>
    fstream time 16<br>
    c file pointer time 0<br>
    memory mapping time 265</p>
    
    <p>File Size 1259530844<br>
    fstream time 31<br>
    c file pointer time 16<br>
    memory mapping time 11138   </p>
  </blockquote>
  
  <p>It seems that for stream and FILE* reading,the time needed to read a file won't increase as the file size increases.But for memory mapping,that's true.This phenomenon is pretty odd.<br>
  Because in my opinion,for large files, memory mapping will faster.
  Here is my code:  </p>
  
  <pre><code>string ifile = "M:/Thesis/FileReadCmp/1.txt";
  string os = "M:/Thesis/FileReadCmp/new_cmp1.txt";
  int page_size = 2 * 1024 * 64 * 1024;//128M
  for (int j = 0; j &lt; 100; ++j){
      os[os.size() - 5] = '1' + j;
      ofstream o(os);
      for (int i = 0; i &lt; 4; ++i){
          ifile[ifile.size() - 5] = '1' + i;
          ifstream in(ifile);
  
          in.seekg(0, ios::end);
          o &lt;&lt; "File Size " &lt;&lt; in.tellg() &lt;&lt; endl;
          o &lt;&lt; endl;
          in.close();
  
          //using fstream to read file
          long long st = GetTickCount();
          in.open(ifile);
          char c;
          while (in &gt;&gt; c){
              ;
          }
          in.close();             
          long long et = GetTickCount();
          o &lt;&lt; "fstream time " &lt;&lt; et - st &lt;&lt; endl;
  
          //using FILE* to read file
          st = GetTickCount();
          FILE* cpf = fopen(ifile.c_str(), "r");
          char cc = fgetc(cpf);
          while (cc != EOF)
          {
              cc = fgetc(cpf);
          }
          fclose(cpf);
          et = GetTickCount();
          o &lt;&lt; "c file pointer time " &lt;&lt; et - st &lt;&lt; endl;
  
          //using memory mapping to read file
          const char* pc = ifile.c_str();
          st = GetTickCount();
          HANDLE hFile = CreateFile(pc, GENERIC_WRITE | GENERIC_READ, 0,
              NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
          int file_size = GetFileSize(hFile, NULL);
          HANDLE hFileMap = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE,
              TEXT("SharedData"));
  
          if (hFileMap == NULL){
              // if no such object,create a file mapping object
              hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE,
                  0, 0, TEXT("SharedData"));
          }
  
              int rem_file_size = file_size;
              int offset = 0;
              while (rem_file_size &gt; page_size){
                  PVOID pvFileView = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, offset, page_size);
                  char* asc_dex = (char*)pvFileView;
                  for (int i = 0; i &lt; page_size; ++i){//, c = 0++c
                      char c = asc_dex[i];
                  }
                  //UnmapViewOfFile(pvFileView);
                  offset += page_size;
                  rem_file_size -= page_size;
              }
              PVOID pvFileView = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, offset, rem_file_size);
              char* asc_dex = (char*)pvFileView;
              for (int i = 0; i &lt; rem_file_size; ++i){//, c = 0++c
                  char c = asc_dex[i];
              }
              UnmapViewOfFile(pvFileView);
  
          CloseHandle(hFileMap);
          CloseHandle(hFile);
  
          et = GetTickCount();
          o &lt;&lt; "memory mapping time " &lt;&lt; et - st &lt;&lt; endl;
          o &lt;&lt; endl;
      }
  }
  </code></pre>