Skip to content

Instantly share code, notes, and snippets.

@eregon

eregon/bench_regexp.rb Secret

Created Oct 24, 2020
Embed
What would you like to do?
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