How text compare works: the diff algorithm and how to read a patch
A text compare tool does not just put two texts side by side. It solves a puzzle: the smallest set of lines to delete and add to turn one text into the other. Here is how that works, why results sometimes look odd, and how to read the patch it produces.
The problem a diff solves
Given an original and a changed text, split into lines, a diff finds the longest common subsequence: the largest set of lines that appear in both texts in the same order. Everything else is either removed (only in the original) or added (only in the new version). Lines are compared whole, so a line with one changed word counts as one line removed and one added; a second, finer pass then highlights the words that changed inside it.
The Myers algorithm
GNU diff, and Git by default, use the method published by Eugene W. Myers in 1986, "An O(ND) Difference Algorithm and Its Variations" (Algorithmica, volume 1, pages 251 to 266). The GNU diffutils manual names it as the basic algorithm of GNU diff. Its key idea: the time it takes grows with the size of the texts (N) times the number of differences (D). Two long, nearly identical texts compare almost instantly; two completely different texts are the slowest case.
The text compare on this site uses the same algorithm. We checked our implementation against a brute-force solution on 3,000 random pairs: it always produced a correct and minimal set of changes, and it compared two 20,000-line texts with a few scattered changes in 4 milliseconds.
Why a diff sometimes looks wrong
- Moved paragraphs show as removed in one place and added in another. Line diffs have no notion of "moved".
- Repeated lines (blank lines, closing brackets, "Thanks,") can be matched to the wrong copy, so a change appears shifted by a few lines. The result is still a minimal set of changes, just not the one a person would pick.
- Whitespace: a reformatted text differs on every line. Use Ignore spaces and Ignore empty lines.
- Very different lines paired as "changed" would be all highlight, so the tool shows them whole instead.
Reading a unified diff (patch)
Copy as patch produces the unified format designed by Wayne Davison and used by diff -u and Git:
--- original +++ changed @@ -3,3 +3,3 @@ The meeting is on -Monday at 10 am. +Tuesday at 11 am. Please bring the report.
- The two header lines name the original (
---) and the changed file (+++). - Each change block, or hunk, starts with
@@ -start,count +start,count @@: the line where the block starts in each file and how many lines it covers. - Lines starting with a space are unchanged context,
-lines were removed and+lines added. - By default, the GNU manual says, three lines of context are shown around each change, and changes closer than that merge into one hunk.
A patch can be applied to the original file with patch or git apply, and it is the
clearest way to send "what changed" in an email or a ticket.
Text compare or list compare?
A diff respects order: it answers "how did this document change?". When order does not matter, such as two lists of emails, a diff marks an item as changed just because it moved. For those, Compare two lists matches every item wherever it is and answers "what is in one list and not the other?". For structured data, JSON compare compares by key and path instead of by line.
Tools for this
- Compare two listsOnly in A, only in B, in both. Excel columns, near matches, CSV.
- Text compareCompare two texts and see every added, removed and changed word.
- JSON compareCompare two JSON files: every changed, added and removed value with its path.