Development

What diff algorithms are available and which should I use?

Different diff algorithms produce different results with different performance characteristics. Learn about available algorithms and how to choose the best one for your needs.

By Inventive HQ Team

Understanding Diff Algorithms

A diff algorithm is the method a comparison tool uses to show changes between two versions of a file. Git offers myers (the default), minimal, patience, and histogram, selectable with git diff --diff-algorithm=<name> or the diff.algorithm config key. Minimal spends more time seeking a smaller diff; patience and histogram use different matching heuristics that can improve readability. For code review, try histogram when the default diff is hard to follow.

That is the summary an AI Overview gives you. What it can't give you is the side-by-side of why the same two files produce different-looking diffs under each engine, a working command reference you can paste, and a tool to feel the difference yourself. That's below.

Diff algorithms are the mathematical engines behind diff tools, determining how differences are identified and displayed. While end users typically don't think about which algorithm is being used, understanding the available algorithms and their trade-offs helps you choose the right diff tool and interpret results correctly.

Different algorithms use different strategies to identify differences, leading to different results with different computational costs. The choice of algorithm affects both the quality of results and the speed of comparison.

Git's Four Diff Algorithms Compared

Everything you can select in Git lives in this table. This is the decision surface — pick a row, run the command, move on.

AlgorithmHow it worksStrengthsWhen to use
myers (default)Uses Myers-style matching with Git's default heuristics.Fast for many common edits; predictable default.Start here for ordinary comparisons.
minimalSpends extra time to make the smallest possible diff.Can remove extra edits left by default matching.When diff size matters more than speed.
patienceMatches lines that appear exactly once in both files first, then compares gaps.Can produce readable diffs when repeated lines confuse default matching.When a Myers diff looks scrambled.
histogramExtends patience-style matching to use low-frequency lines as anchors.Can improve readability, especially with repeated lines.Try for code review when default matching is unclear.

Set your preference once:

# Recommended for code review
git config --global diff.algorithm histogram

# Or per-invocation (overrides config)
git diff --diff-algorithm=patience file.js

# Shortcut flags exist for three of them
git diff --histogram
git diff --patience
git diff --minimal
How Myers and histogram diff a reordered code block A valid line diff for moving gamma ahead of alpha and beta. Both algorithms may represent the move as one deletion and one addition. Same reordered file, two algorithms

myers (default) + function gamma() { } function alpha() { } function beta() { } - function gamma() { } (other lines unchanged) 1 deletion, 1 addition

histogram + function gamma() { } function alpha() { } function beta() { } - function gamma() { } 1 addition, 1 deletion

display can vary with surrounding lines

The Classic Longest Common Subsequence (LCS) Algorithm

How LCS Works

The Longest Common Subsequence algorithm finds the longest sequence of items that appears in both files in the same order:

  1. Compare all possible subsequences in both files
  2. Find the longest subsequence appearing in both
  3. Lines not in the LCS are differences
  4. Display what was added, removed, and kept

LCS guarantees finding the minimal-change diff, meaning fewest lines marked as different.

Advantages of LCS

  • Optimal results: Minimizes the number of differences shown
  • Predictable: Always produces the same result for the same input
  • Mathematically sound: Provably correct approach

Disadvantages of LCS

  • Slow: O(n×m) time complexity where n and m are file sizes
  • Memory intensive: Requires storing comparison data for all possible subsequences
  • Can be costly for large files: A basic dynamic-programming table grows with both input lengths

When to Use LCS

LCS is suitable for:

  • Small files
  • When guaranteed optimal results are important
  • Academic or research purposes
  • When performance is less critical than correctness

Most practical diff tools have moved away from pure LCS due to performance limitations.

The Myers Algorithm (Diff Algorithm)

How Myers Algorithm Works

Myers algorithm finds the shortest "edit script" with fewer operations required:

  1. Creates a search space of possible edit paths
  2. Explores the space to find shortest path to make files identical
  3. This shortest-edit objective corresponds to finding a longest common subsequence
  4. More efficient exploration reduces unnecessary comparisons
Advertisement

Advantages of Myers

  • Often faster than a basic LCS table: Its search can be efficient when files differ little
  • Practical for large files: Works efficiently on real-world file sizes
  • Better for version control: Produces intuitive results for code changes
  • Shortest-edit basis: The underlying algorithm finds a shortest edit script; Git's default mode also uses heuristics
  • Industry standard: Used by Git and most modern diff tools

Disadvantages of Myers

  • More complex: Harder to understand than naive approaches
  • Still slow for very different files: Degrades when files differ significantly

When to Use Myers

Myers algorithm is suitable for:

  • Most real-world use cases
  • Version control systems (Git uses this)
  • Code comparison
  • Files of any practical size
  • When both correctness and performance matter

Git's diff command and most modern tools default to Myers or variants.

Patience Algorithm

How Patience Algorithm Works

Patience algorithm uses a different approach inspired by card game solitaire:

  1. Lines with unique tokens are matched first (patience matching)
  2. Builds blocks of matching content
  3. Recursively applies algorithm to unmatched regions
  4. Produces line-based differences; it does not understand code semantics

Advantages of Patience

  • Better for code: Produces more intuitive results for source code changes
  • Handles some refactoring diffs better: Can keep clearer matching context around reordered code
  • Faster for certain patterns: Efficient when files have repeated sections
  • Readable in some cases: Changes can make more sense to humans

Disadvantages of Patience

  • Not optimal: May not find absolute minimum differences
  • Less predictable: Results depend on content, not just structure
  • More opinionated: Biases results toward certain interpretations

When to Use Patience

Patience algorithm is excellent for:

  • Code comparison and review
  • Understanding refactoring changes
  • Making diffs more human-readable
  • Files with repeated content blocks
  • Reviewing line changes where unique lines provide useful anchors

Histogram Algorithm

How Histogram Works

Histogram algorithm is a refinement of patience algorithm:

  1. Creates frequency histograms of lines
  2. Matches lines with unique or rare tokens first
  3. Recursively processes remaining lines
  4. Can perform well on files with repeated lines

Advantages of Histogram

  • Efficient in some cases: Performance depends on the input
  • Handles large files: Works well on substantial codebases
  • Good heuristic: Produces human-sensible results
  • Performance optimized: Uses memory-efficient data structures

Disadvantages of Histogram

  • Heuristic-based: Not guaranteed optimal
  • Content dependent: Results vary based on actual file content

When to Use Histogram

Histogram is ideal for:

  • Large source code files
  • When performance and readability matter equally
  • Production version control systems
  • Git can use histogram: git diff --histogram

Git's histogram algorithm is an optimized variant used in practice for better performance.

Other Specialized Algorithms

Unified Diff Format Algorithm

Not a comparison algorithm but a representation format that can work with various backends:

  • Shows differences with context lines
  • Standard format understood across tools
  • Used in patch files
  • Can be generated by multiple algorithms

Split View Algorithm

Not an algorithm itself but a display strategy:

  • Shows original on left, modified on right
  • Highlights differences in both versions
  • User alignment of changed sections
  • More visual than text-based diffs

Practical Algorithm Selection

For Git Users

Configure your preferred algorithm:

# Use patience algorithm
git config --global diff.algorithm patience

# Use histogram algorithm
git config --global diff.algorithm histogram

# Use Myers (default)
git config --global diff.algorithm myers

Git also supports:

  • default - Myers algorithm
  • myers - Myers algorithm explicitly
  • minimal - Myers with minimal diff size
  • patience - Patience algorithm
  • histogram - Histogram algorithm

For Online Diff Tools

Most online diff tools support:

  • Myers: Standard for compatibility
  • Patience: Better for code
  • Histogram: Balanced performance/quality

Check your tool's documentation for options.

Decision Tree for Algorithm Selection

  1. Performance critical and large files? → Use Histogram
  2. Code comparison and readability important? → Use Patience or Histogram
  3. Small files and correctness critical? → Use LCS or Myers
  4. Default for most cases? → Use Myers or Histogram
  5. Unsure? → Start with Git's default; try Histogram if the result is hard to read

Performance Characteristics Comparison

AlgorithmSpeedOptimalBest For
LCSSlowestYesSmall files, guaranteed correctness
MyersVariesGit default may use heuristicsGeneral purpose, version control
PatienceModerateNoCode readability
HistogramFastNoLarge files, balanced approach

Real-World Algorithm Behavior

Example: Moved Code Block

Consider code that moves from one location to another:

// Original
function alpha() { }
function beta() { }
function gamma() { }

// Modified
function gamma() { }
function alpha() { }
function beta() { }

Possible diff: Add gamma before alpha and remove its old line after beta; alpha and beta remain unchanged.

Patience and histogram may choose different matching lines when surrounding code contains repeated content.

Configuring Your Diff Tool

Command-Line Configuration

Most tools allow algorithm selection:

# Git with histogram (recommended)
git diff --histogram

# Diff command with options
diff -u file1 file2

# Meld visual diff tool (uses advanced algorithms)
meld file1 file2

GUI Tool Selection

Visual diff tools often handle algorithm selection automatically:

  • Modern tools default to Histogram or Patience
  • Some allow switching algorithms in preferences
  • Better tools adapt algorithm based on file type

Whitespace and Diff Algorithms

All algorithms must handle whitespace considerations:

  • Ignore all whitespace: -w flag
  • Ignore change of whitespace: -b flag
  • Ignore blank lines: -B flag
  • Show all whitespace: Default

Whitespace handling is orthogonal to algorithm choice.

Testing Different Algorithms

For critical comparisons, test multiple algorithms:

# Compare results with different algorithms
git diff --diff-algorithm=myers file.js
git diff --diff-algorithm=patience file.js
git diff --diff-algorithm=histogram file.js

Prefer to see it rather than run it? Paste two versions of a file below and compare them instantly in the browser — no install, nothing leaves your machine.

Interactive toolDiff CheckerRuns in your browser — nothing is uploaded.

When results differ significantly, investigate which makes the most sense for your context.

Conclusion

Different diff algorithms produce different results with different performance characteristics. While users typically don't select algorithms explicitly, understanding the options helps you:

  • Configure Git optimally for your workflows
  • Understand why different diff tools show slightly different results
  • Make informed choices when results seem unexpected
  • Optimize performance for your specific use cases

For most modern users, the default Myers algorithm or Git's Histogram variant serves well. For code-specific use cases, Patience or Histogram algorithms often produce more intuitive results. Understanding these options empowers you to make informed decisions about your diff tool configuration and interpret results accurately.

The key takeaway: there's no single "best" algorithm for all scenarios. Choose based on your specific needs: guaranteed optimality, speed, code readability, or a balanced combination.

Frequently Asked Questions

What diff algorithms does Git support?

Git supports four diff modes selectable with git diff --diff-algorithm= or the diff.algorithm config key: myers (the default), minimal (which spends extra time seeking a smaller diff), patience, and histogram. Patience and histogram use line matching heuristics that can make diffs easier to read.

Which diff algorithm should I use?

Start with Git's default myers mode. Try histogram or patience if the displayed diff is hard to follow, and minimal when a smaller diff matters more than speed. Results and performance depend on the files being compared.

What is the difference between patience and histogram diff?

Patience starts with lines unique to both files. Histogram extends that approach by considering low-frequency common lines, giving it more possible anchors when lines repeat. Either may produce a clearer diff for a particular file; neither tracks moved code as a separate operation.

Is histogram or Myers better for Git?

Histogram can make some code changes easier to review by choosing different matching lines. Myers is Git's default, but Git's minimal mode may find a smaller diff. Neither changes the file content that Git commits; the choice affects the displayed diff and patch shape.

How do I change the diff algorithm in Git permanently?

Run git config --global diff.algorithm histogram to set it for all repositories, or drop --global to set it per-repo. You can still override per command with git diff --diff-algorithm=patience. The setting also influences git show, git log -p, and merge base comparisons.

Does the diff algorithm change what Git commits or merges?

No. The diff algorithm only affects how differences are computed for display and for generating patch text. The committed content, blob hashes, and history are identical regardless of algorithm. It can influence merge results indirectly (git's merge uses diff internally), but for normal add/commit workflows the choice is purely cosmetic and about readability.

What is the Myers diff algorithm?

The Myers algorithm, published by Eugene Myers in 1986, finds a shortest edit script for sequences. Git's default diff mode uses Myers-style matching with heuristics; its separate minimal mode can spend more time finding a smaller diff. Myers is efficient when changes are small.

Why do two diff tools show different results for the same files?

Because they use different algorithms or heuristics. All correct diff algorithms produce a valid set of changes, but there are usually many valid ways to describe the same edit. Patience and histogram can choose different matching lines from Git's default Myers mode. Whitespace and end-of-line settings add further variation.

diff algorithmscode comparisonalgorithm selectionperformance optimizationdevelopment tools