Created
August 26, 2015 00:22
-
-
Save keichan34/e48bb35efc37b0861095 to your computer and use it in GitHub Desktop.
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
~/Desktop > ruby --version | |
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14] | |
~/Desktop > ruby ./concat_benchmark.rb | |
Calculating ------------------------------------- | |
Interpolation 81.309k i/100ms | |
Array + join 65.714k i/100ms | |
String concatenation 62.816k i/100ms | |
String mutation 68.529k i/100ms | |
------------------------------------------------- | |
Interpolation 1.843M (± 6.0%) i/s - 9.188M | |
Array + join 1.218M (± 5.8%) i/s - 6.111M | |
String concatenation 1.087M (± 5.1%) i/s - 5.465M | |
String mutation 1.248M (± 5.0%) i/s - 6.236M |
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
#!/usr/bin/env ruby | |
require 'benchmark/ips' | |
STRING = "hello there".freeze | |
Benchmark.ips do |x| | |
x.report("Interpolation") do | |
"#{STRING}-#{STRING}-#{STRING}" | |
end | |
x.report("Array + join") do | |
[STRING, STRING, STRING].join("-") | |
end | |
x.report("String concatenation") do | |
STRING + "-" + STRING + "-" + STRING | |
end | |
x.report("String mutation") do | |
str = "" | |
str << STRING | |
str << "-" | |
str << STRING | |
str << "-" | |
str << STRING | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment