Command line

Are These Two Files the Same? cmp vs diff, Measured — Plain diff Costs 100x on Text and Nothing Extra on Binary

Four ways to ask whether two files are identical: cmp -s, diff -q, plain diff, and sha256sum as the reference arm. On binary files all three comparison tools cost the same. On text files plain diff pays the full line-diff cost on every case, even when the difference is on line 1. The equality check is -q, not diff itself.

Written November 9, 2026 · 14 min read

By the end of this guide
  • The table: two corpora, four cases, four arms
  • On binary files there is no tool winner
  • --speed-large-files buys nothing at this scale
  • sha256sum is the slow reference arm, and it never compares anything

What this guide is

Two corpora, four cases each, four arms. The binary corpus is one 1 GiB file of random bytes with four variants: an identical copy, the first byte flipped, the last byte flipped, and a copy one byte shorter. The text corpus is 1,000,000 lines, 91 MB, with four variants: an identical copy, line 1 changed, line 500001 changed, and the last line dropped. The arms are cmp -s, diff -q, plain diff, and sha256sum on binary or diff --speed-large-files on text.

The question is the one people type in scripts: are these two files the same? The answer splits by file type, and both halves matter. On binary files there is no tool winner at all. On text files plain diff is the wrong tool for the question, and it costs 5 to 100 times more than diff -q depending on where the difference sits.

The two tools, briefly

cmp compares two files byte by byte. The manual page states it plainly, and the -s flag suppresses all normal output, so the answer is the exit status alone: 0 if the inputs are the same, 1 if different, 2 if trouble. That is the whole interface for an equality check: silent, scriptable, one number out.

diff compares files line by line. The -q flag, --brief, reports only whether the files differ, not how. Without it, diff computes and prints the line-by-line differences, which is a different job from answering yes or no. The --speed-large-files flag tells diff to assume large files and many scattered small changes. It does not turn diff into an equality check; the numbers below show it does not even make the full diff cheaper at this scale.

The two tools also differ in what they do with binary files, and the manual explains why. The GNU diffutils manual says that when diff thinks either file is binary, it normally treats the pair much as if the summary output format had been selected, and reports only that the binary files are different, because line-by-line comparisons are usually not meaningful for binary files. It decides text or binary by checking the first few bytes for null bytes. That is why plain diff inherits the early exit and the size shortcut on binary files for free, and why it pays the full cost on text.

The table: two corpora, four cases, four arms

Wall seconds, medians of three reps, fresh tonight.

Binary, 1 GiB, two files compared. sha256sum hashes both files, so it reads everything on every case.

  • IDENT, identical copy: cmp -s 0.448, diff -q 0.450, plain diff 0.436, sha256sum 1.119. Reps tonight: cmp 0.434, 0.448, 0.512; diff -q 0.442, 0.450, 0.457; diff 0.432, 0.436, 0.438; sha256sum 1.115, 1.119, 1.119. Night before: 0.445, 0.433, 0.431, 1.131.
  • EARLY, first byte flipped: cmp -s 0.002, diff -q 0.002, plain diff 0.002, sha256sum 1.139. Reps tonight: all three comparison arms 0.002 on all three reps; sha256sum 1.128, 1.139, 1.172. Night before: 0.002, 0.002, 0.002, 1.106.
  • LATE, last byte flipped: cmp -s 0.433, diff -q 0.436, plain diff 0.440, sha256sum 1.109. Reps tonight: cmp 0.432, 0.433, 0.480; diff -q 0.434, 0.436, 0.444; diff 0.430, 0.440, 0.447; sha256sum 1.105, 1.109, 1.120. Night before: 0.431, 0.431, 0.424, 1.102.
  • SHORT, second file one byte shorter: cmp -s 0.002, diff -q 0.002, plain diff 0.002, sha256sum 1.112. Reps tonight: all three comparison arms 0.002 on all three reps; sha256sum 1.106, 1.112, 1.149. Night before: 0.002, 0.002, 0.002, 1.102.

Text, 1,000,000 lines, 91 MB. The fourth arm here is diff --speed-large-files, not sha256sum.

  • IDENT, identical copy: cmp -s 0.041, diff -q 0.039, plain diff 0.190, diff --speed-large-files 0.170. Reps tonight: cmp 0.039, 0.041, 0.044; diff -q 0.038, 0.039, 0.039; diff 0.175, 0.190, 0.230; speed-large 0.132, 0.170, 0.187. Night before: 0.041, 0.041, 0.190, 0.145.
  • EARLY, line 1 changed: cmp -s 0.002, diff -q 0.002, plain diff 0.202, diff --speed-large-files 0.169. Reps tonight: cmp 0.002 on all three reps; diff -q 0.002, 0.002, 0.003; diff 0.199, 0.202, 0.203; speed-large 0.168, 0.169, 0.194. Night before: 0.002, 0.002, 0.201, 0.178.
  • MID, line 500001 changed: cmp -s 0.020, diff -q 0.021, plain diff 0.199, diff --speed-large-files 0.158. Reps tonight: cmp 0.020, 0.020, 0.021; diff -q 0.020, 0.021, 0.021; diff 0.166, 0.199, 0.249; speed-large 0.158, 0.158, 0.162. Night before: 0.021, 0.021, 0.162, 0.161.
  • SHORT, last line dropped: cmp -s 0.002, diff -q 0.002, plain diff 0.155, diff --speed-large-files 0.154. Reps tonight: cmp 0.002 on all three reps; diff -q 0.002 on all three reps; diff 0.151, 0.155, 0.191; speed-large 0.154, 0.154, 0.175. Night before: 0.002, 0.002, 0.156, 0.164.

Correctness, from the untimed pass after the timed runs. Exit status 0 on every identical case and 1 on every differing case, for all four arms on both corpora. cmp -s prints nothing on every case. diff -q prints one line, 67 to 69 bytes: "Files ... differ". Plain diff on binary prints one line, 75 to 76 bytes: "Binary files ... differ". Plain diff on text prints the change: 194 bytes and 4 lines on EARLY, 204 bytes and 4 lines on MID, naming line 500001, and 108 bytes and 2 lines on SHORT. diff --speed-large-files prints exactly the same bytes as plain diff on every text case. sha256sum returns 0 on every binary case, because it hashes, it never compares.

The trap: plain diff never early-exits on text

Trap
On text, plain diff pays the full read plus the line-diff cost on every case. EARLY is the sharpest cell: the difference is on line 1, and plain diff still takes 0.202 seconds against 0.002 for diff -q, about 100 times more, in both runs. It reads both files fully and computes the diff even though the answer was decided at the first line. MID costs 0.199 against 0.021, about 8 to 10 times more across the two runs. SHORT costs 0.155 against 0.002, about 78 times more, even though the files differ in size and plain diff still reads both fully. IDENT costs 0.190 against 0.041, about 4.6 times more in both runs.

The pattern is the finding. diff file1 file2 in a script, written to ask whether two files are the same, pays the full diff cost every time it runs. diff -q or cmp -s answers the same question for the early-exit cost. The guide's headline is the command, not the ratio: the equality check is -q, not diff itself.

Two cells in the table moved between runs, and the guide keeps both numbers visible. The MID plain-diff median is 0.199 tonight against 0.162 last night, with tonight's reps spanning 0.166 to 0.249, so the MID ratio is 9.5 times tonight and 7.7 times last night, and the guide states the range rather than one figure. The --speed-large-files IDENT median is 0.170 tonight against 0.145 last night, so the flag's best case does not reproduce; the flag section below gives the range across both runs.

On binary files there is no tool winner

On the 1 GiB binary corpus, cmp -s, diff -q, and plain diff cost the same on every case. IDENT is 0.448, 0.450, and 0.436 tonight, and 0.445, 0.433, and 0.431 last night. EARLY is 0.002 for all three in both runs. LATE is 0.433, 0.436, and 0.440 tonight, and 0.431, 0.431, and 0.424 last night. SHORT is 0.002 for all three in both runs.

The reason is in the manual, and the probe confirms it. diff detects binary files and reports only that they differ, without computing a line diff, so it inherits the early exit and the size shortcut for free. The 0.002-second cells are the process-spawn floor, not a measured comparison cost: the tools return before reading the file, and the ratio against a full read is order-of-magnitude, not precise. Do not quote "100x" for the binary EARLY cell as a measured speedup; the honest statement is that all three return at the spawn floor and sha256sum takes about 550 to 570 times longer because it hashes both files in full.

The choice between cmp -s and diff -q on binary files is output format and scripting idiom, not speed. cmp -s prints nothing; diff -q prints one line naming the files. Both return the same exit codes, and both cost the same on every case measured.

--speed-large-files buys nothing at this scale

The flag's medians against plain diff on the text corpus, tonight and last night: IDENT 0.170 against 0.190 tonight and 0.145 against 0.190 last night; EARLY 0.169 against 0.202 tonight and 0.178 against 0.201 last night; MID 0.158 against 0.199 tonight and 0.161 against 0.162 last night; SHORT 0.154 against 0.155 tonight and 0.164 against 0.156 last night. Across both runs the flag runs at 0.76 to 1.05 times the cost of plain diff: small, inconsistent, and sometimes slower.

It also does not make plain diff an equality check. With the flag, the text cells still cost 0.154 to 0.170 seconds on every case, against 0.002 for diff -q on the early and short cases. The flag assumes large files and many scattered small changes, which is a different problem from asking whether two files are the same. Do not present the flag as the fix for a slow equality check; the fix is -q.

sha256sum is the slow reference arm, and it never compares anything

On the binary corpus, sha256sum takes about 1.10 to 1.14 seconds on every case, regardless of where the difference is. Against cmp -s on IDENT it is 2.50 times slower tonight and 2.54 times last night. On EARLY it is about 550 to 570 times slower across the two runs, because it hashes both files in full while the comparison tools return at the spawn floor. Its exit status is 0 on every case, identical or not, because hashing is not comparing: two hashes that differ tell you the files differ, and two that match tell you they almost certainly do not, but the tool itself never answers the question.

That is also the limit of what this guide says about hashing. sha256sum appears here as one reference arm, not as a comparison. The throughput ranking of hash tools against each other is a different question, and this guide does not re-teach it. Hashing earns its place when the files live on different machines, or when a published checksum is the thing being verified, or when a fingerprint has to be stored and checked later. For a local, one-off equality check, it is the slowest arm on the table by a wide margin.

What the numbers do not say

  • The 0.002-second cells are at the process-spawn floor. Report them as about instant, never as a precise denominator. The 100x and 78x ratios on text are order-of-magnitude, from medians; the MID ratio is a range, 7.7 to 9.5 times across the two runs, because the plain-diff MID median moved.
  • tmpfs only. Both corpora live on /dev/shm, a RAM filesystem. Cold-disk behavior, spinning disks, USB sticks, and NVMe are unmeasured.
  • Warm cache only. The source files are generated once per run and read repeatedly; the page cache is warm for every timed rep. A cold read of files not in cache will be slower in absolute terms.
  • Single-threaded, one corpus size per type. One 1 GiB binary file and one 1,000,000-line text file. Smaller files, many scattered differences, and directory trees are different questions. Plain diff on text with many scattered differences can only get slower than the IDENT cell measured here; that is unmeasured, not assumed.
  • The nearest published comparison asserts the answer with no numbers. A copyprogramming guide states that diff -sq matches cmp -s performance in benchmarks and that hashing is noticeably slower, but it publishes no timings, file sizes, or repetitions. A Linux Mint forum thread cites a Unix Stack Exchange test secondhand, with no table. This probe is the first measured table for this trio on sized corpora, and its text-versus-binary split is not stated anywhere found.
  • diff --speed-large-files was measured at this scale only: one 1 GiB binary file and one 91 MB text file. Larger files with many scattered changes are the flag's stated use case, and this probe does not test them.

Checklist

  • Before asking whether two files are the same with diff, add -q: plain diff on text costs about 100 times more than diff -q when the difference is on line 1, and 78 times more when the sizes differ.
  • Before choosing between cmp -s and diff -q on speed, check the table: they are cost-identical on every case measured, so choose on output format, not speed.
  • Before reaching for --speed-large-files to fix a slow equality check, check the flag: it runs at 0.76 to 1.05 times plain diff at this scale and never turns diff into an equality check.
  • Before hashing two local files to compare them, check the reference arm: sha256sum takes about 1.1 seconds on every binary case and never compares anything, so it is the slowest way to answer a yes-or-no question.
  • Before trusting a 0.002-second cell as a speedup, check the floor: it is the process-spawn floor, and the ratios built on it are order-of-magnitude, not precise.
  • Before citing the nearest published comparison, check its method: the copyprogramming guide asserts the answer with no timings, sizes, or repetitions, and the forum thread cites a test secondhand with no table.
  • Before reusing these numbers, check the setup: WSL2, 32 CPUs, diffutils 3.10, tmpfs, warm cache, one 1 GiB binary file and one 1,000,000-line text file, one thread per arm — the directions held across both runs, the absolute seconds are this box's.

Sources

  • Linux man-pages project, cmp(1) — read on the box 2026-11-09: cmp compares two files byte by byte; -s suppresses all normal output; exit status 0 if inputs are the same, 1 if different, 2 if trouble.
  • Linux man-pages project, diff(1) — read on the box 2026-11-09: diff compares files line by line; -q reports only whether files differ; --speed-large-files assumes large files and many scattered small changes; exit status 0 if inputs are the same, 1 if different, 2 if trouble.
  • GNU Diffutils manual, Binary Files and Forcing Text Comparisons — read 2026-11-09, version 3.12, 12 January 2025: when diff thinks either file is binary, it normally treats the pair much as if the summary output format had been selected and reports only that the binary files are different; text or binary is decided by checking the first few bytes for null bytes; -q reports only whether files differ.
  • copyprogramming, Comparing Two Files in the Linux Terminal in 2026 — read in full 2026-11-09: the nearest published comparison, asserted rather than measured — diff -sq matching cmp -s performance in benchmarks with no timings, sizes, or repetitions published, and hashing noticeably slower for the same reason; --speed-large-files recommended for huge text files with no measured table.
  • Veldica, Copying a 1 GiB File: dd at bs=512 Is 9x Slower — the sibling guide: the same tmpfs and warm-cache harness, a different question — copying a file, not comparing two files; its cmp -s correctness checks are the same tool this guide measures.

Keep Exploring