Skip to content

Instantly share code, notes, and snippets.

@k0kubun
Last active September 15, 2019 17:32
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 k0kubun/9c2c705be681c98984be97ad6a5b96bd to your computer and use it in GitHub Desktop.
Save k0kubun/9c2c705be681c98984be97ad6a5b96bd to your computer and use it in GitHub Desktop.
require 'benchmark_driver'
Benchmark.driver do |x|
x.prelude <<~EOS
# frozen_string_literal: true
@world = 'world'
EOS
x.report 'array', %q{
buf = []
buf << @world.to_s
buf << 'hello, '
buf << @world.to_s
buf.join
}
x.report 'hybrid', %q{
buf = []
buf << "#{@world}hello, #{@world}"
buf.join
}
end
Warming up --------------------------------------
array 3.387M i/s - 3.518M times in 1.038515s (295.23ns/i)
hybrid 3.702M i/s - 3.714M times in 1.003258s (270.12ns/i)
Calculating -------------------------------------
array 3.599M i/s - 10.161M times in 2.823170s (277.83ns/i)
hybrid 3.840M i/s - 11.106M times in 2.892639s (260.45ns/i)
Comparison:
hybrid: 3839502.9 i/s
array: 3599286.1 i/s - 1.07x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment