Skip to content

Instantly share code, notes, and snippets.

@e0da
Created August 6, 2015 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e0da/ac67bcec1e5e09da32a4 to your computer and use it in GitHub Desktop.
Save e0da/ac67bcec1e5e09da32a4 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
STRINGS = %w[
There's no beginning to this story. A bookshelf sinks into the sand. And a
language learned, a forgotten term, is studied once again. It's a shocking
bit of footage viewed from a shitty TV screen. Just squint at it, just snowy
static to make out the meaning. And keep on stretching the antenna hoping that
it will come clear. We need some reception, a higher message. Just tell us
what to fear 'cause I don't know what tomorrow brings. It's alive with such
possibility. All I know is I feel better when I sing.
]
def concatenate
STRINGS.inject('') {|memo, string| memo + string}
end
def join
STRINGS.join
end
Benchmark.ips do |x|
x.report('Concatenate') { concatenate }
x.report('Join') { join }
x.compare!
end
Calculating -------------------------------------
Concatenate 2.753k i/100ms
Join 17.199k i/100ms
-------------------------------------------------
Concatenate 28.549k (± 5.6%) i/s - 143.156k
Join 200.620k (± 4.9%) i/s - 1.015M
Comparison:
Join: 200620.3 i/s
Concatenate: 28548.9 i/s - 7.03x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment