Created
December 16, 2013 15:16
-
-
Save kwleland/7988659 to your computer and use it in GitHub Desktop.
String#[]= benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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