Skip to content

Instantly share code, notes, and snippets.

@sue445
Created December 12, 2023 02:56
Show Gist options
  • Save sue445/bca485a762a23b49d239db877650fb83 to your computer and use it in GitHub Desktop.
Save sue445/bca485a762a23b49d239db877650fb83 to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "benchmark-ips"
end
I = "Rubyist"
PHPER = "PHPer"
puts RUBY_DESCRIPTION
puts ""
Benchmark.ips do |x|
x.report("String#replace") { I.replace PHPER }
x.report("String#gsub!") { I.gsub!(I, PHPER) }
x.report("String#[]=") { I[0..-1] = PHPER }
end
ruby 3.3.0rc1 (2023-12-11 master a49643340e) [arm64-darwin23]
Warming up --------------------------------------
String#replace 2.298M i/100ms
String#gsub! 384.013k i/100ms
String#[]= 1.599M i/100ms
Calculating -------------------------------------
String#replace 23.031M (± 0.3%) i/s - 117.219M in 5.089779s
String#gsub! 3.851M (± 0.6%) i/s - 19.585M in 5.085532s
String#[]= 16.063M (± 0.4%) i/s - 81.545M in 5.076715s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment