Skip to content

Instantly share code, notes, and snippets.

@bheeshmar
Created April 9, 2011 22:10
Show Gist options
  • Save bheeshmar/911825 to your computer and use it in GitHub Desktop.
Save bheeshmar/911825 to your computer and use it in GitHub Desktop.
Benchmarking various ways to grep recursively
require 'benchmark'
Benchmark.bm(15) do |test|
test.report('grep-recursive') do
system('grep -r --include="*.h" -l include /usr/src/linux-headers-2.6.32-30/ > /dev/null')
end
test.report('grep-find') do
system('grep -l include $(find /usr/src/linux-headers-2.6.32-30/ -name "*.h" -print) > /dev/null')
end
test.report('find-xargs-grep') do
system('find /usr/src/linux-headers-2.6.32-30/ -name "*.h" -print0 | xargs -0 grep -l include > /dev/null')
end
test.report('ruby-oneliner') do
system('ruby -e "Dir[\'/usr/src/linux-headers-2.6.32-30/**/*.h\'].each { |f| puts f unless open(f).grep(/include/).empty? }" > /dev/null')
end
test.report('find-exec-grep') do
system('find /usr/src/linux-headers-2.6.32-30/ -name "*.h" -exec grep -l include "{}" \; > /dev/null')
end
end
# Results
#
# $ ruby grep_bench.rb
# user system total real
# grep-recursive 0.000000 0.000000 0.140000 ( 0.126757)
# grep-find 0.000000 0.000000 0.140000 ( 0.120633)
# find-xargs-grep 0.000000 0.000000 0.200000 ( 0.119407)
# ruby-oneliner 0.000000 0.000000 0.920000 ( 0.906290)
# find-exec-grep 0.000000 0.000000 78.840000 ( 22.173068)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment