title: C - Speeding Up Binary File IO
body: <p>I'm currently using the following code to parse a binary file byte by byte to look for occurrences of 0x44 0x44 0x53 0x20. The files range from 100 to 500 mb.</p>
  <pre class="lang-c prettyprint-override"><code>
  int main() {
      ...
      FILE *f = fopen(name, &quot;rb&quot;);
  
      fseek(f, 0, SEEK_END);
      int size = ftell(f);
      parse_file(f, size);
      ...
  }
  
  void parse_file(FILE *f, int size) {
      fseek(f, 0, SEEK_SET);
      int read = 0;
      int count = 0;
  
      uint8_t *buf = malloc(size);
      fread(buf, sizeof(uint8_t), size, f);
      while (read &lt; size - sizeof(uint32_t)) {
          if (*(buf + read) == 0x44) {
              if (*(buf + read + 1) == 0x44) {
                  if (*(buf + read + 2) == 0x53) {
                      if (*(buf + read + 3) == 0x20) {
                          count++;
                      }
                  }
              }
          }
          printf(&quot;\r%d/%d (%.2f%%)&quot;, read, size, (double)read / size);
          read += sizeof(uint8_t);
      }
      free(buf);
      buf = NULL;
  }
  </code></pre>
  <p>The first file is 105mb and it only processes ~0.01% per minute. This seems much slower then I would have expected. Is it possible to optimize the code to speed the process up significantly? I'm using MinGW_64 on Windows 10 with 16gb of memory (at 33% usage with program running)</p>
  <p>EDIT:
  Code I was using before which was only slightly slower:</p>
  <pre class="lang-c prettyprint-override"><code>while (read &lt; (size - sizeof(uint32_t))) {
      uint32_t bytes;
      fread(&amp;bytes, sizeof(uint32_t), 1, f);
      if (bytes == 0x44445320) {
          count++;
      }
      fseek(f, -(sizeof(uint8_t)*3), SEEK_CUR);
      read += sizeof(uint8_t);
      printf(&quot;\r%d/%d (%.2f%%)&quot;, read, size, (double)read / size);
  }
  </code></pre>