BENCHMARKS · REGEX

One pattern, three engines: interpreted, compiled, source-generated. Suite and raw reports: github.com/gdhami-net/dotnet-benchmarks.

Intel Core Ultra 9 285HX · Windows 11 · BenchmarkDotNet 0.15.8 · SDK 10.0.201 · 2026-09-01 · median of 4 quiet runs · typical spread ±3.3% · bars/tables: mean time, lower is better

EmailMatching

The same email pattern over 5,000 mixed lines: interpreted vs Compiled vs source-generated regex.

TAKEAWAY A static [GeneratedRegex] is the right default: compiled-class speed, no startup JIT cost, AOT-compatible. Never new up a Regex per call.
In this run: SourceGen_regex is fastest — 1.8× faster than the baseline.
RESULTS
methodnet9.0net10.0ratioallocatedΔ net10.0
Interpreted_regex baseline254.2 µs ±920 ns248.7 µs ±1.4 µs1.00×—-2%
Compiled_regex152.3 µs ±65 ns147.1 µs ±92 ns0.59×—-3%
SourceGen_regex122.9 µs ±99 ns140.1 µs ±87 ns0.56×—+14%
RATIO VS BASELINE · net10.0
Interpreted_regex1.00×
Compiled_regex0.59×
SourceGen_regex0.56×
THE LEDGER · SAME WORKLOAD ACROSS RELEASES (LOG)
Interpreted_regexCompiled_regexSourceGen_regex
288.2 µs189.7 µs124.8 µs10.0.201 · 08-22 v210.0.201 · 09-01 v3
THE CODE BEING MEASURED
Interpreted_regex — what this measures

new Regex(pattern) — interpreted at match time.

[Benchmark (Baseline)]
public int Interpreted_regex()
{
    var hits = 0;
    foreach (var l in _lines)
        if (Interpreted.IsMatch(l)) hits++;
    return hits;
}
Compiled_regex — what this measures

RegexOptions.Compiled — IL emitted at construction.

[Benchmark]
public int Compiled_regex()
{
    var hits = 0;
    foreach (var l in _lines)
        if (Compiled.IsMatch(l)) hits++;
    return hits;
}
SourceGen_regex — what this measures

[GeneratedRegex] — C# generated at compile time, AOT-friendly.

[Benchmark]
public int SourceGen_regex()
{
    var hits = 0;
    foreach (var l in _lines)
        if (SourceGen().IsMatch(l)) hits++;
    return hits;
}