Skip to content

Instantly share code, notes, and snippets.

@kwleland
Created December 16, 2013 15:16
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 kwleland/7988659 to your computer and use it in GitHub Desktop.
Save kwleland/7988659 to your computer and use it in GitHub Desktop.
String#[]= benchmark
require 'benchmark'
require 'benchmark/ips'
Benchmark.ips do |x|
s = "abcdefghijk"
last_index = s.length - 1
middle_index = last_index/2
x.report("element assignment at beginning") do |times|
i = 0
while i < times
s[0] = "x"
i += 1
end
end
x.report("element assignment in middle") do |times|
i = 0
while i < times
s[middle_index] = "x"
i += 1
end
end
x.report("element assignment at end") do |times|
i = 0
while i < times
s[last_index] = "x"
i += 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment