Skip to content

Instantly share code, notes, and snippets.

@enebo
Last active November 7, 2019 19:55
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 enebo/2dd71dbe8dd8a786427d1e2c2962a90a to your computer and use it in GitHub Desktop.
Save enebo/2dd71dbe8dd8a786427d1e2c2962a90a to your computer and use it in GitHub Desktop.
# coding: utf-8
require 'benchmark/ips'
STR1 = 'white chocolate あand white xmas and white bread'
STR2 = 'a1'
STR3 = 'dog'
REGEXP2 = /\d/
REGEXP3 = /\w+/
def one
i = 1
while i < 100
STR1.gsub('white', 'dark')
i += 1
end
end
def two
i = 1
while i < 100
STR2.gsub(REGEXP2, '2')
i += 1
end
end
def three
i = 1
while i < 100
STR3.gsub(REGEXP3) { |animal| animal == 'dog' ? 'cat' : 'dog' }
i += 1
end
end
Benchmark.ips do |x|
x.config warmup: 20
x.report 'gsub-string' do
one
end
x.report "gsub-regex" do
two
end
x.report "gsub-regex-block" do
three
end
end
=== 9.2.9.0 ===
system ~/work/jruby master 1609% jruby ../snippets/gsub1.rb
Ignoring bcrypt-ruby-3.0.0-java because its extensions are not built. Try: gem pristine bcrypt-ruby --version 3.0.0
Warming up --------------------------------------
gsub-string 1.162k i/100ms
gsub-regex 3.155k i/100ms
gsub-regex-block 2.066k i/100ms
Calculating -------------------------------------
gsub-string 12.228k (± 3.1%) i/s - 61.586k in 5.041594s
gsub-regex 32.789k (± 3.2%) i/s - 164.060k in 5.008918s
gsub-regex-block 21.609k (± 2.8%) i/s - 109.498k in 5.071486s
=== branch ===
system ~/work/jruby use-string-matching-5905 1606% jruby ../snippets/gsub1.rb
Ignoring bcrypt-ruby-3.0.0-java because its extensions are not built. Try: gem pristine bcrypt-ruby --version 3.0.0
Warming up --------------------------------------
gsub-string 1.654k i/100ms
gsub-regex 2.898k i/100ms
gsub-regex-block 1.967k i/100ms
Calculating -------------------------------------
gsub-string 16.692k (± 5.2%) i/s - 84.354k in 5.068474s
gsub-regex 30.225k (± 7.6%) i/s - 150.696k in 5.018490s
gsub-regex-block 20.939k (± 6.6%) i/s - 104.251k in 5.003751s
=== branch + patch ===
system ~/work/jruby use-string-matching-5905 * 1613% jruby ../snippets/gsub1.rb
Ignoring bcrypt-ruby-3.0.0-java because its extensions are not built. Try: gem pristine bcrypt-ruby --version 3.0.0
Warming up --------------------------------------
gsub-string 2.138k i/100ms
gsub-regex 3.326k i/100ms
gsub-regex-block 2.225k i/100ms
Calculating -------------------------------------
gsub-string 23.338k (± 2.5%) i/s - 117.590k in 5.041663s
gsub-regex 34.066k (± 3.9%) i/s - 172.952k in 5.085821s
gsub-regex-block 22.621k (± 3.0%) i/s - 113.475k in 5.021033s
=== MRI 2.7 ===
Warming up --------------------------------------
gsub-string 906.000 i/100ms
gsub-regex 945.000 i/100ms
gsub-regex-block 859.000 i/100ms
Calculating -------------------------------------
gsub-string 8.642k (± 8.9%) i/s - 43.488k in 5.075977s
gsub-regex 9.365k (± 8.4%) i/s - 47.250k in 5.085282s
gsub-regex-block 8.316k (± 8.5%) i/s - 42.091k in 5.098502s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment