Skip to content

Instantly share code, notes, and snippets.

@ggrossetie
Created October 3, 2019 13:51
Show Gist options
  • Save ggrossetie/ee400ee15d163b3392c27dcd86aee8d2 to your computer and use it in GitHub Desktop.
Save ggrossetie/ee400ee15d163b3392c27dcd86aee8d2 to your computer and use it in GitHub Desktop.
gsub benchmark TruffleRuby (19.2.0) vs MRI (Ruby 2.6.2)
str1 = 'white chocolate'
str2 = 'a1'
str3 = 'dog'
regex2 = /\d/
regex3 = /\w+/
benchmark 'gsub-string' do
r1 = str1.gsub('white', 'dark')
end
benchmark "gsub-regex" do
r2 = str2.gsub(regex2, '2')
end
benchmark "gsub-regex-block" do
r3 = str3.gsub(regex3) { |animal| animal == 'dog' ? 'cat' : 'dog' }
end
Warming up --------------------------------------
gsub-string 133.332k i/100ms
gsub-regex 109.422k i/100ms
gsub-regex-block 100.462k i/100ms
gsub-string 133.908k i/100ms
gsub-regex 109.548k i/100ms
gsub-regex-block 100.390k i/100ms
gsub-string 134.102k i/100ms
gsub-regex 110.124k i/100ms
gsub-regex-block 100.716k i/100ms
Calculating -------------------------------------
gsub-string 1.604M (± 4.4%) i/s - 8.046M in 5.027993s
gsub-regex 1.275M (± 1.1%) i/s - 6.387M in 5.008714s
gsub-regex-block 1.196M (± 0.9%) i/s - 6.043M in 5.052229s
gsub-string 1.640M (± 2.0%) i/s - 8.314M in 5.072339s
gsub-regex 1.268M (± 1.9%) i/s - 6.387M in 5.039050s
gsub-regex-block 1.191M (± 1.9%) i/s - 6.043M in 5.075145s
gsub-string 1.616M (± 1.7%) i/s - 8.180M in 5.064058s
gsub-regex 1.271M (± 1.3%) i/s - 6.387M in 5.024287s
gsub-regex-block 1.202M (± 1.1%) i/s - 6.043M in 5.030043s
Comparison:
gsub-string: 1615810.2 i/s
gsub-regex: 1271467.8 i/s - 1.27x slower
gsub-regex-block: 1201523.3 i/s - 1.34x slower
Warming up --------------------------------------
gsub-string 29.634k i/100ms
gsub-regex 154.576k i/100ms
gsub-regex-block 123.275k i/100ms
gsub-string 39.857k i/100ms
gsub-regex 227.180k i/100ms
gsub-regex-block 188.622k i/100ms
gsub-string 38.142k i/100ms
gsub-regex 218.579k i/100ms
gsub-regex-block 200.674k i/100ms
Calculating -------------------------------------
gsub-string 387.474k (±30.6%) i/s - 1.640M in 5.051508s
gsub-regex 2.225M (±21.4%) i/s - 10.055M in 4.999286s
gsub-regex-block 2.093M (±23.3%) i/s - 9.432M in 5.053969s
gsub-string 426.231k (±20.2%) i/s - 1.983M in 5.024651s
gsub-regex 2.327M (±18.8%) i/s - 10.710M in 5.008312s
gsub-regex-block 2.283M (±22.1%) i/s - 10.435M in 5.057132s
gsub-string 447.039k (±22.2%) i/s - 2.060M in 5.107442s
gsub-regex 2.362M (±18.3%) i/s - 10.929M in 5.029235s
gsub-regex-block 2.288M (±22.0%) i/s - 10.435M in 5.041204s
Comparison:
gsub-regex: 2361636.1 i/s
gsub-regex-block: 2288310.2 i/s - same-ish: difference falls within error
gsub-string: 447039.4 i/s - 5.28x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment