#!/usr/bin/env ruby require 'better-benchmark' num_inner_iterations = 1_000_000 puts "Single Quote Append vs. Double Quote Append" result = Benchmark.compare_realtime( :verbose => true ) { x = '' num_inner_iterations.times do |i| x = 'This is a test ' << i.to_s end }.with { x = "" num_inner_iterations.times do |i| x = "This is a test " << i.to_s end } Benchmark.report_on result puts "\nAppend vs. Interpolation" result = Benchmark.compare_realtime( :verbose => true ) { x = "" num_inner_iterations.times do |i| x = "This is a test " << i.to_s end }.with { x = "" num_inner_iterations.times do |i| x = "This is a test #{i}" end } Benchmark.report_on result puts "\nTwo Appends vs. Interpolation" result = Benchmark.compare_realtime( :verbose => true ) { x = "" num_inner_iterations.times do |i| x = "This is a test " << i.to_s << "x" end }.with { x = "" num_inner_iterations.times do |i| x = "This is a test #{i}x" end } Benchmark.report_on result puts "\nConcatenation vs. Interpolation" result = Benchmark.compare_realtime( :verbose => true ) { x = "" num_inner_iterations.times do |i| x = "This is a test " + i.to_s end }.with { x = "" num_inner_iterations.times do |i| x = "This is a test #{i}" end } Benchmark.report_on result puts "\nTwo Concatenations vs. Interpolation" result = Benchmark.compare_realtime( :verbose => true ) { x = "" num_inner_iterations.times do |i| x = "This is a test " + i.to_s + "x" end }.with { x = "" num_inner_iterations.times do |i| x = "This is a test #{i}x" end } Benchmark.report_on result puts "\nConcatenation vs. Append" result = Benchmark.compare_realtime( :verbose => true ) { x = "" num_inner_iterations.times do |i| x = "This is a test " + i.to_s end }.with { x = "" num_inner_iterations.times do |i| x = "This is a test " << i.to_s end } Benchmark.report_on result num_inner_iterations = 300_000 puts "\nSeveral Concatenations vs. Several Appends" result = Benchmark.compare_realtime( :verbose => true ) { x = "" num_inner_iterations.times do |i| x = "This is a test " + i.to_s + "This is a test " + i.to_s + "This is a test " + i.to_s + "This is a test " + i.to_s + "This is a test " + i.to_s end }.with { x = "" num_inner_iterations.times do |i| x = "This is a test " << i.to_s << "This is a test " << i.to_s << "This is a test " << i.to_s << "This is a test " << i.to_s << "This is a test " << i.to_s end } Benchmark.report_on result