Skip to content

Instantly share code, notes, and snippets.

@mohnish
Created March 29, 2016 20:15
Show Gist options
  • Save mohnish/1bae41798f1a956917ec8b0911e039bb to your computer and use it in GitHub Desktop.
Save mohnish/1bae41798f1a956917ec8b0911e039bb to your computer and use it in GitHub Desktop.
benchmark string concat vs append vs +
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
@mohnish
Copy link
Author

mohnish commented Mar 29, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment