Created
March 29, 2016 20:15
-
-
Save mohnish/1bae41798f1a956917ec8b0911e039bb to your computer and use it in GitHub Desktop.
benchmark string concat vs append vs +
This file contains hidden or 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/ips' | |
n = 100_000 | |
Benchmark.ips do |x| | |
x.config(time: 5, warmup: 2) | |
x.report('concat') do |times| | |
foo = "" | |
n.times do |i| | |
foo.concat("#{i}") | |
end | |
end | |
x.report('append') do |times| | |
foo = "" | |
n.times do |i| | |
foo << "#{i}" | |
end | |
end | |
x.report('+') do |times| | |
foo = "" | |
n.times do |i| | |
foo += "#{i}" | |
end | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Darwin mt-4.local 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64