grep -F vs -E vs -P, Measured: Same Pattern, Three Engines, One Silent Trap
grep -F and grep -E tie at 0.040 seconds on a plain literal, and grep -P costs 0.099 for the same 49,533 lines — 2.5 times more for nothing. LC_ALL=C halves -P and barely moves -E. And on a dotted service name the regex engines silently return 10,058 lines the fixed-string search does not, with no error and no warning. The flag is not a speed choice. It is a correctness choice that happens to have a speed shape.
Written November 9, 2026 · 16 min read
- The table: -F and -E tie on a plain literal, and -P costs 2.5 times more
- LCALL=C halves -P and barely moves -E
- On a real regex, -P and -E tie — and the direction flips overnight
- The ripgrep lead did not reproduce
What this guide is
One file, one million lines, three patterns, three engines, three reps each. The corpus is a generated log — 82,947,324 bytes in /dev/shm, one million lines shaped 2026-11-09T10:MM:SSZ level=<level> service=<service> user=<user> msg=<msg>, seed 20261109. Every arm searches the same file with the same pattern and writes its output to tmpfs, and every output is checked by line count and by sha256, so the table compares engines that returned the same lines, not three different answers — except where the trap is the point, and then the different line count is the finding.
The three patterns ask three different questions. Pattern A is a plain literal, level=error: does the fixed-string flag buy anything when the pattern has no metacharacters at all? Pattern B is a metachar literal, service=auth.api: what happens when the dots in the pattern are meant literally and the engine is allowed to read them as wildcards? Pattern C is a real regex, user=u[0-9]{5}: when the pattern genuinely needs a regex engine, is -P still the slow one? The corpus is poisoned on purpose for pattern B — 1 percent of lines carry service=authXapi, a name the dotted pattern must not match — and 10 percent of users are 3-digit, so pattern C matches a subset. The expected counts are fixed at generation: A 49,533, B under -F 399,913, B under -E/-P 409,971, C 899,333 — and every rep hit its expected count exactly.
The table: -F and -E tie on a plain literal, and -P costs 2.5 times more
Wall seconds, medians of three runs, GNU grep 3.11, pattern A level=error, 49,533 lines out, all six arms byte-identical (sha b07d460dab5b2829).
grep -F level=error: fresh median 0.0396, reps 0.0394, 0.0396, 0.0430.grep -E level=error: fresh median 0.0390, reps 0.0387, 0.0390, 0.0394.grep -P level=error: fresh median 0.0990, reps 0.0979, 0.0990, 0.1016.grep -E level=error,LC_ALL=C: fresh median 0.0383, reps 0.0382, 0.0383, 0.0388.grep -P level=error,LC_ALL=C: fresh median 0.0528, reps 0.0527, 0.0528, 0.0530.rg -F level=error: fresh median 0.0513, reps 0.0503, 0.0513, 0.0517.
The -F vs -E tie is the first finding, and it held on both nights: the banked run measured -F 0.0410 against -E 0.0396, the fresh run 0.0396 against 0.0390. The difference is inside rep noise either way. The rule everyone repeats — fixed strings are faster — does not hold for a single literal on GNU grep 3.11. John D. Cook measured the same shape: on a single pattern, fgrep was a tiny bit faster than grep, on the order of 1 percent, not enough to notice. Modern grep is smart enough to search for a literal string quickly without being told explicitly with -F that the pattern is a string. The -F flag's real value on a plain literal is not speed. It is the next section.
The -P tax is the second finding, and it held on both nights too: banked 0.0986 against -F 0.0410, fresh 0.0990 against 0.0396 — 2.4 times one night, 2.5 times the other, for the same 49,533 lines out. -P hands the pattern to the PCRE engine, and the engine costs what it costs even when the pattern has nothing Perl-specific in it. Reaching for -P by habit on a plain search is the slow path, and the habit is common because -P is also the flag that unlocks \d, \w, and lookarounds — so it feels like the powerful default. On a literal it is the expensive default. The grep(1) manual page defines the three flags plainly: -F interprets patterns as fixed strings, not regular expressions; -E interprets them as extended regular expressions; -P interprets them as Perl-compatible regular expressions. Nothing in the definitions promises -P is fast. Nothing in the measurement says it is.
LC_ALL=C halves -P and barely moves -E
The locale arms are the third finding. LC_ALL=C on pattern A cut -P from 0.0990 to 0.0528 — a 47 percent cut, and the banked night measured the same shape, 0.0986 to 0.0525. The same setting moved -E from 0.0390 to 0.0383 — a 2 percent cut, inside noise. The banked night gave -E a 7 percent cut, 0.0396 to 0.0368, which is also small. Either way the locale is a -P story, not an -E story.
The mechanism is the one the manuals describe: the default locale makes grep do locale-aware character processing, and LC_ALL=C forces byte-by-byte ASCII comparison. That work matters to the PCRE engine and barely matters to the ERE engine on ASCII text. The nearest published pages state the locale effect much more strongly than this measurement supports — one claims 20 to 100 times faster, another 40 percent on a 500 MB file — and those numbers are for different files and different patterns, stated without reps. On this corpus, on this pattern, the measured locale effect is 47 percent on -P and about nothing on -E. If you are reaching for LC_ALL=C to speed up a plain -E search, the table says you are fixing the wrong arm. Reach for it when the search is already -P, or switch the search off -P.
The trap: 10,058 lines that should not be there
service=auth.api, is where the flag choice stops being about speed. Wall seconds, medians of three runs:grep -F service=auth.api: fresh median 0.0739, reps 0.0734, 0.0739, 0.0740 — 399,913 lines, shae1d4ab2d39f08a36.grep -E service=auth.api: fresh median 0.0946, reps 0.0933, 0.0946, 0.0954 — 409,971 lines, sha004c441a573a0961.grep -P service=auth.api: fresh median 0.1415, reps 0.1405, 0.1415, 0.1422 — 409,971 lines, the same sha as -E.rg -F service=auth.api: fresh median 0.0918, reps 0.0907, 0.0918, 0.0926 — 399,913 lines, the same sha as grep -F.
The -E and -P arms return 409,971 lines. The -F arms return 399,913. The difference is 10,058 lines — 2.5 percent of the -E output — and every one of them is an authXapi poison line, where the X sits exactly where the pattern's dot is. Under -E and -P the dot is a wildcard, so auth.api matches authXapi, auth0api, and anything else with one character between auth and api. Under -F the dot is a dot. There is no error, no warning, and no short line count to catch it — the -E output is longer, not shorter, and it looks exactly like a correct result with more matches. The line-count check is the only tell, and it only works because the corpus was generated with a known count. On a real log file, where nobody knows the true count in advance, the 10,058 extra lines are invisible.
The speed ranking on pattern B is also worth stating plainly, because it cuts against the usual advice: -F is the fastest arm at 0.0739, then -E at 0.0946 (1.28 times -F), then -P at 0.1415 (1.91 times -F). The flag that is correct here is also the fastest, and the flag that is wrong is also the slowest — but the speed is not the reason to choose -F. The reason is that -E and -P answer a different question than the one asked, and they do it silently. The IT Playbook grep guide states the trap correctly — without -F, every . in an IP address or version string is a wildcard, so grep 10.0.0.1 also matches 1000x010 — and then asserts -F is both correct and faster. The measurement agrees on this pattern. It does not agree on pattern A, where -F is not faster. Correctness is the reliable half of that advice. Speed is the contingent half.
On a real regex, -P and -E tie — and the direction flips overnight
Pattern C, user=u[0-9]{5}, cannot be expressed with -F at all, so the comparison is -E against -P. Wall seconds, medians of three runs, 899,333 lines out, both arms byte-identical (sha 2ca1acdefcab2ff7).
grep -E 'user=u[0-9]{5}': fresh median 0.1671, reps 0.1668, 0.1671, 0.1698.grep -P 'user=u[0-9]{5}': fresh median 0.1785, reps 0.1775, 0.1785, 0.1785.
Fresh, -P is 6.8 percent slower than -E. Banked, -P was 6.9 percent faster — -E 0.1890 against -P 0.1760. The direction flipped between nights, and the spread inside each night is wider than the gap between the medians. There is no winner here. -P is not universally slower — it is slower exactly where the pattern needs no regex engine, which is patterns A and B, and it ties -E where the pattern genuinely needs one. Anyone who concludes from pattern A that -P is always 2.5 times slower is overreading the table by exactly one pattern. The honest claim is narrower: -P costs 2.5 times more on a literal, and ties on a real regex.
The ripgrep lead did not reproduce
The banked run measured rg -F as the fastest arm on both literal patterns: 0.0209 on pattern A against grep -F 0.0410, and 0.0633 on pattern B against grep -F 0.0765. The fresh run does not reproduce either number. Fresh, rg -F is 0.0513 on pattern A and 0.0918 on pattern B — slower than grep -F on both, 1.30 times slower on A and 1.24 times slower on B, with three tight reps each (0.0503 to 0.0517 on A, 0.0907 to 0.0926 on B). The banked pattern-A median rested on two fast reps, 0.0190 and 0.0209, next to a cold outlier of 0.0684 — the median of three where one rep is cold and two are fast is fragile, and the fresh run's three consistent reps are the more trustworthy shape.
This guide does not claim ripgrep is slow. It claims the banked rg lead is not a stable claim, and the fresh numbers are the ones that reproduce. The design difference points the same way: ripgrep's speed comes from searching many files in parallel and skipping what .gitignore excludes, and neither applies to one file on tmpfs with one thread per arm. On a single file the comparison is startup plus engine, and, tonight, grep -F wins that comparison. On a repository, the answer is different — and it is not measured here.
What the nearest guides get wrong
The nearest published page on the three variants, the copyprogramming grep vs egrep vs fgrep guide, asserts that fgrep is the fastest option for literal strings, that its Aho-Corasick algorithm guarantees linear time, and that on a 5 GB log file fgrep cuts the search from 50-plus seconds to under 30. It also claims LC_ALL=C buys 40 percent or more on large files. None of those numbers comes with a corpus, a pattern, reps, or a same-pattern comparison — they are assertions, not measurements — and the same-pattern table in this guide contradicts the headline claim on its own terms: on a plain literal, -F ties -E at 0.040 seconds, and the 40-percent locale claim holds for -P (47 percent) and not for -E (2 percent). The page's advice to default to -F for literal strings is still right, but for the wrong reason: -F is right because it is correct on dotted literals, as pattern B shows, not because it is faster on plain ones, as pattern A shows it is not.
The same page's ripgrep section claims 2 to 10 times faster than grep on large codebases, and that claim is about codebases — many files, parallel search — which this guide does not measure and does not dispute. The backtracking guide on this site compares grep -P against grep -E and ripgrep on the pathological (a+)+$ pattern, where -P takes 16 seconds and the others stay flat. That is a backtracking question, not an engine-choice question on a plain search: it never tests -F, and it never tests a metachar literal. This guide asks the question that one does not, and cross-links it rather than reteaching it.
How to tell which flag fits
The choice is not speed. -F and -E tie on a plain literal, -P costs 2.5 times more on that same literal, and the ranking reverses on a dotted literal, where -F is both fastest and the only correct answer. Choose by what the pattern means, then check the speed table for the pattern you actually have.
Use -F when the pattern is a literal string — an IP address, a version string, a service name with dots, a path. The pattern is taken literally, every dot is a dot, and nothing extra matches. Use -E when the pattern needs grouping, alternation, or bounded repetition, and the pattern has no literal dots you forgot to escape. Use -P only when ERE cannot express what you need — lookarounds, non-greedy quantifiers, \d and \w — and confirm GNU grep is present before depending on it, because BSD and macOS grep do not support -P at all. If the search is already -P and slow, LC_ALL=C is the first thing to try: it cut -P nearly in half here. If the search is -E and slow, the locale will not save it — the pattern or the corpus is the problem, not the locale.
And when the output looks right, count it. The pattern-B trap is the shape of failure that no flag fixes after the fact: -E and -P returned more lines than the truth, silently, and the only check that caught it was knowing how many lines there should have been. On a real log file, the check is a spot look at the output — one line in the middle, one line at the end — and the question is whether every line you see is a line you asked for.
What this guide does not cover
No -f pattern-file comparison is measured here: Hacker Public Radio tested that shape and found a single pattern no faster under fgrep, but a file with a couple dozen patterns about 20 percent faster — a different question from one pattern on the command line, and the one Cook's ~1 percent result also points at. No multi-file or recursive search is measured: ripgrep's parallel advantage lives there, and this corpus is one file. No --mmap, no --line-buffered, no binary-file behavior. No BSD grep: the box runs GNU grep 3.11, and -P does not exist on macOS or BSD grep at all. The corpus is 1 million lines on tmpfs with a warm page cache — the absolute seconds will differ on a real disk, and the ranking is the claim, not the seconds.
Checklist
- Before searching for a literal string with dots in it, check the flag: use -F, because -E and -P read every dot as a wildcard and return lines you did not ask for — 10,058 extra on a million-line file here, silently.
- Before reaching for -P on a plain search, check the pattern: if it has no lookarounds, no non-greedy quantifiers, and no
\dor\w, -P costs 2.5 times more than -F or -E for the same lines out. - Before assuming -F is faster than -E, check the table: on a plain literal they tie at 0.040 seconds, and the ~1 percent gap Cook measured is the same shape — -F is the correct flag on literals, not the fast one.
- Before adding LC_ALL=C to speed up a search, check which engine you are speeding: it cut -P by 47 percent and moved -E by 2 percent, so it is a -P fix, not a general one.
- Before trusting a grep output, count it: spot-check one line in the middle and one at the end, and ask whether every line you see is a line you asked for — the trap output looks exactly like a correct result with more matches.
- Before quoting a ratio, check the night: the pattern-C direction flipped between the banked and fresh runs, and the ripgrep lead did not reproduce, so the stable claims are the tie, the -P tax, and the locale split — not the one-night edges.
Related guides on this site
- mv Across Filesystems, Measured: One Rename or a Full Copy
- Pulling One Field Out of a Million JSON Lines: jq vs Python vs grep, Measured
- Numbering a Million Lines, Measured: cat -n vs nl vs awk vs grep -n
- Finding Lines by Prefix: look vs grep vs rg vs awk vs sed, Measured
Sources
- grep(1), Linux manual page — read 2026-11-09: -F interprets patterns as fixed strings, not regular expressions; -E interprets them as extended regular expressions; -P interprets them as Perl-compatible regular expressions, and is experimental when combined with -z. Box runs GNU grep 3.11; the box man page was read fresh the same day and says the same.
- John D. Cook, Is fgrep much faster than grep? — read 2026-11-09: on a single pattern, fgrep was a tiny bit faster than grep, on the order of 1 percent, not enough to notice; the author suspects grep has gotten smart enough to search for strings quickly without being told with -F.
- Hacker Public Radio, UNIX Curio #2 — fgrep — read 2026-11-09: a single pattern under fgrep was not really any faster than grep; with a -f file holding a couple dozen patterns, fgrep consistently saved about 20 percent.
- IT Playbook, grep and Regular Expressions — read 2026-11-09: BRE/ERE/PCRE dialects and what each escapes; -F makes a dotted literal both correct and faster, asserted without timings; LC_ALL=C drops Unicode collation work on byte-oriented scans; -P is a GNU extension, absent on BSD and macOS grep.
- copyprogramming, Grep vs Egrep vs Fgrep — read 2026-11-09: the nearest published page — asserts fgrep is fastest via Aho-Corasick, a 5 GB file cut from 50-plus seconds to under 30, and LC_ALL=C gains of 40 percent or more, with no corpus, no pattern, no reps, and no same-pattern table; its ripgrep section claims 2 to 10 times faster on codebases, a multi-file question this guide does not measure.
- Veldica, The Regex That Hangs: Catastrophic Backtracking, Measured Across Five Engines — the sibling guide: grep -P against grep -E and ripgrep on the pathological
(a+)+$pattern, where -P takes 16 seconds and the others stay flat — a backtracking question, not an engine-choice question on a plain search; it never tests -F and never tests a metachar literal. - Veldica, Pulling One Field Out of a Million JSON Lines: jq vs Python vs grep, Measured — the sibling guide: grep as the extraction winner on a million JSON lines — a different job, extraction, not engine choice.
Keep Exploring
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.
Paste sshd -G output and get a hardening verdict on the values your server actually runs.
Paste a server block and get WebSocket, TLS, HSTS, and proxy-header verdicts.
Paste a systemd timer expression and get the specific reason it fails, the normalized form, and the past-date traps the built-in check hides.
The full sysadmin hub \u2014 systemd, cgroups, file tools, and measured Linux comparisons.