Skip to content

Instantly share code, notes, and snippets.

@eregon
Created October 24, 2020 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eregon/998ecc6c4e18ee1dca8c71b23eeb934c to your computer and use it in GitHub Desktop.
Save eregon/998ecc6c4e18ee1dca8c71b23eeb934c to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
# require 'benchmark-memory'
arr = %w[foobar foobaz bazquux hello world just making this array longer]
REGEXP = /o/
def select_match(arr)
arr.select { |e| e.match?(REGEXP) }
end
def grep(arr)
arr.grep(REGEXP)
# arr.select { |e| REGEXP === e }
end
Benchmark.ips do |x|
x.iterations = 2
x.report("select.match?") { select_match(arr) }
x.report("grep") { grep(arr) }
x.compare!
end
# puts "********* MEMORY *********"
#
# Benchmark.memory do |x|
# x.report("select.match?") { select_match(arr) }
# x.report("grep") { grep(arr) }
# x.compare!
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment