grep is one of the most commonly used utilities on Linux. ack is a faster replacement of grep written purely in portable Perl 5 and takes advantage of the power of Perl’s regular expressions. ack is optimized for searching version controlled source code.
Ag or The Silver Searcher (ag chemically represents the element silver) is an optimized replacement for ack. It is 3 to 5 times faster than ack and targets source code search. It ignores file patterns from directories created by version control systems. If there are files in your source repo you don’t want to search, just add their patterns to a .agignore file.
The author explains the tweaks that make Ag so fast:
- Searching for literals (no regex) uses Boyer-Moore-Horspool strstr.
- Files are mmap()ed instead of read into a buffer.
- If built with PCRE 8.21 or greater, regex searches use the JIT compiler.
- Ag calls pcre_study() before executing the regex on a jillion files.
- Instead of calling fnmatch() on every pattern in ignore files, non-regex patterns are loaded into an array and binary searched.
- Ag uses Pthreads to take advantage of multiple CPU cores and search files in parallel.
Tempted to try it out? To install Ag on Ubuntu:
$ sudo apt-get install silversearcher-ag
You can integrate Ag in vim using the ack.vim plugin. Add the following line to your .vimrc:
let g:ackprg = 'ag --nogroup --nocolor --column'
The cmdline options are similar to grep. A common search using Ag is:
$ ag -anr "search_string" *
a: include all files
n: show line numbers
r: search recursivel
Here are my benchmarks with grep and Ag:
$ time grep -nr "fprintf" *
$ time ag -anr "fprintf" *
Real is wall clock time – time from start to finish of the call.
User is the amount of CPU time spent in user-mode code (outside the kernel) within the process.
Sys is the amount of CPU time spent in the kernel within the process.
The results are consistent. Ag does more processing but completes faster (Real time) than grep.
- jrep is a grep-like utility powered by regular expression compiler rejit.