Skip to content

Instantly share code, notes, and snippets.

@mayuresh-srivastava
Forked from tbuehlmann/string_sub_bm.rb
Created October 7, 2019 09:59
Show Gist options
  • Save mayuresh-srivastava/5ffc82aa88d75050a0e0592b25197b09 to your computer and use it in GitHub Desktop.
Save mayuresh-srivastava/5ffc82aa88d75050a0e0592b25197b09 to your computer and use it in GitHub Desktop.
require 'benchmark'
N = 10_000_000
Benchmark.bmbm do |bm|
bm.report('String') do
N.times do
a = 'foobarfoobarhmm'
a.sub('foo', 'BAR')
end
end
bm.report('Regexp') do
N.times do
a = 'foobarfoobarhmm'
a.sub(/foo/, 'BAR')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment