Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Last active October 7, 2019 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tbuehlmann/5574713 to your computer and use it in GitHub Desktop.
Save tbuehlmann/5574713 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
@yarkm13
Copy link

yarkm13 commented Jul 25, 2018

Interesting result. It is surprise for me that RefExp version can be faster. I was using string for simple cases always.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment