title: Fast way to check if a large set of file pairs are identical
body: <p>I'm building a small sync utility in C++, for personal use mostly.</p>
  
  <p>Imagine we have two directories 'A' and 'B' that will be synced. At some point, the new files from A will have to be copied to B. The logic I used so far was:</p>
  
  <pre><code>browse directory 'A'
  for each 'A/afile'
      copy A/afile to B/afile
  endfor
  for each 'A/adirectory'
      recurse into 'A/adirectory'
  endfor
  </code></pre>
  
  <p>This worked well, until I noticed that with the method above, ALL of the files from A are copied EVERY TIME to B. So, I would only like to perform the copy operation if A/afile and B/afile are different.</p>
  
  <p>So, my question is, how can I compare them in a fast and cross-platform (hopefully) manner? Will something like computing MD5 checksums for each file be fast?</p>
  
  <p>The point is, since the file comparison will be probably done for a large number of file pairs, I want something that is both reliable and fast. And by fast I mean that the 'heavy and time-consuming' task should be the actual copy operation and not the file checking.</p>
  
  <p>PS. I also tried finding 'tricks', like comparing file size and modification times, with no success.</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>After taking into consideration the answers below, what I will finally go with to check if the two files are the same is:</p>
  
  <pre><code>if optimize_speed then
        if A/afile is newer then no (cause A/afile is the 'source' file)
        if B/afile is newer then compare byte-to-byte and decide 
  else
        compare byte-to-byte and decide
  end
  </code></pre>