Skip to content

Instantly share code, notes, and snippets.

@tomtomecek
Last active August 27, 2018 10:05
Show Gist options
  • Save tomtomecek/be467df44da205c49778 to your computer and use it in GitHub Desktop.
Save tomtomecek/be467df44da205c49778 to your computer and use it in GitHub Desktop.
Ruby String interpolation vs Concatenation - benchmarking and comparison

Test nr.1

require 'benchmark/ips'
 
Benchmark.ips do |bm|
 
  TEST_STRING = 'I am testing...'
  ADD_STRING  = 'testing 1 2 3'
 
  bm.time = 5
  bm.warmup = 2
 
  bm.report('using string concatenation') { TEST_STRING + ADD_STRING }
  bm.report('using string interpolation') { "#{ TEST_STRING }#{ ADD_STRING }" }
 
  bm.compare!
end

produces these results:

Calculating -------------------------------------
using string concatenation
                       108.440k i/100ms
using string interpolation
                       102.594k i/100ms
-------------------------------------------------
using string concatenation
                          2.836M (± 2.5%) i/s -     14.206M
using string interpolation
                          2.478M (± 3.0%) i/s -     12.414M

Comparison:
using string concatenation:  2836008.4 i/s
using string interpolation:  2478343.5 i/s - 1.14x slower

Test nr. 2

require 'benchmark/ips'
 
Benchmark.ips do |bm|
  add_string = 'testing 1 2 3'
 
  bm.time = 5
  bm.warmup = 2
 
  bm.report('using string concatenation') { "I am testing..." + add_string }
  bm.report('using string interpolation') { "I am testing...#{add_string}" }
 
  bm.compare!
end

produces these results:

Calculating -------------------------------------
using string concatenation
                       105.379k i/100ms
using string interpolation
                       107.157k i/100ms
-------------------------------------------------
using string concatenation
                          2.419M (± 2.9%) i/s -     12.119M
using string interpolation
                          2.526M (± 2.4%) i/s -     12.645M

Comparison:
using string interpolation:  2525501.9 i/s
using string concatenation:  2418575.7 i/s - 1.04x slower

Test nr.3

require 'benchmark/ips'
 
Benchmark.ips do |bm|
  add_string = 'testing 1 2 3'
 
  bm.time = 5
  bm.warmup = 2
 
  bm.report('using string concatenation') { "I am testing..." + add_string + "...!"}
  bm.report('using string interpolation') { "I am testing...#{add_string}...!" }
 
  bm.compare!
end

produces these results:

Calculating -------------------------------------
using string concatenation
                        76.848k i/100ms
using string interpolation
                        98.816k i/100ms
-------------------------------------------------
using string concatenation
                          1.425M (± 3.0%) i/s -      7.147M
using string interpolation
                          2.229M (± 2.0%) i/s -     11.166M

Comparison:
using string interpolation:  2229133.6 i/s
using string concatenation:  1425011.2 i/s - 1.56x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment