Is cmp faster than diff -q

diff()performance

In a recent Ask Ubuntu question on checking whether some files have differing content, I saw a comment stating that, if the differing sections didn't matter, cmp would be faster than diff. This Stack Overflow answer concurs, giving the reason that cmp would stop at the first differing byte. However, GNU diff has the -q (or --brief) flag, which is supposed to make it report only when files differ. It would seem logical that GNU diff would also stop comparing once any difference is found (like grep would stop searching after the first match when -l or -q is specified).

Is cmp truly faster than diff -q, in the context of Linux systems, which are likely to have the GNU version?

Best Answer

Prompted by @josten, I ran a comparison on the two. The code is on GitHub. In short1:

user-sys real

The User+Sys time taken by cmp -s seemed to be a tad more than that of diff in most cases. However, the Real time take was pretty much arbitrary - cmp ahead on some, diff ahead on some.

Summary:

Any difference in performance is pure coincidence. Use whatever you wish.

1The images are 1920x450, so do open them in a tab to see them in their full glory.

Related Question