Skip to content

Instantly share code, notes, and snippets.

@sugamasao
Created September 18, 2020 08:49
Show Gist options
  • Save sugamasao/bcc35800a895ee39997c09e28cb8f01f to your computer and use it in GitHub Desktop.
Save sugamasao/bcc35800a895ee39997c09e28cb8f01f to your computer and use it in GitHub Desktop.
delete suffix
require 'benchmark/ips'
TIME = 10_00
STR = "ruby"
puts RUBY_VERSION
Benchmark.ips do |bm|
bm.report "gsub" do
TIME.times do
STR.gsub('by', '')
end
end
bm.report "sub" do
TIME.times do
STR.sub('by', '')
end
end
bm.report "delete_suffix" do
TIME.times do
STR.delete_suffix('by')
end
end
bm.compare!
end
2.7.1
Warming up --------------------------------------
gsub 146.000 i/100ms
sub 299.000 i/100ms
delete_suffix 676.000 i/100ms
Calculating -------------------------------------
gsub 1.423k (± 7.2%) i/s - 7.154k in 5.061408s
sub 2.956k (± 4.8%) i/s - 14.950k in 5.070308s
delete_suffix 6.955k (± 4.1%) i/s - 35.152k in 5.063523s
Comparison:
delete_suffix: 6955.1 i/s
sub: 2956.0 i/s - 2.35x (± 0.00) slower
gsub: 1422.7 i/s - 4.89x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment