Skip to content

why is grep > /dev/null so much faster than rg > /dev/null? #2717

Answered by blyxxyz
Mr-Pine asked this question in Q&A
Discussion options

You must be logged in to vote

GNU grep can tell that its output is redirected to /dev/null and because of that it exits after the first match. (It still needs to use the exit code to report whether there was a match at all, but that's it.) If you send the output through cat then it cannot perform that optimization and ends up slightly slower than ripgrep:

$ hyperfine 'rg system rockyou.txt | cat' 'grep system rockyou.txt | cat'
Benchmark #1: rg system rockyou.txt | cat
  Time (mean ± σ):      50.4 ms ±  10.8 ms    [User: 37.6 ms, System: 14.2 ms]
  Range (min … max):    43.0 ms …  84.6 ms    34 runs
 
Benchmark #2: grep system rockyou.txt | cat
  Time (mean ± σ):      71.8 ms ±  13.3 ms    [User: 54.3 ms, System: 18.9…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2715 on January 19, 2024 12:43.