Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created January 23, 2020 05:01
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 havenwood/da6528f48865c135c221812a3c493e01 to your computer and use it in GitHub Desktop.
Save havenwood/da6528f48865c135c221812a3c493e01 to your computer and use it in GitHub Desktop.
Benchmark of various solutions to a question on #ruby IRC
# frozen_string_literal: true
require 'benchmark/ips'
Benchmark.ips do |ips|
words = [*'a'..'all'].freeze
separator = '-'
ips.report('map & join') { words.map { |word| separator + word }.join }
ips.report('join & prepend') { words.join(separator).prepend(separator) }
ips.report('prepend & join') { words.dup.prepend(separator).join(separator) }
ips.report('reduce') { words.reduce('') { |acc, word| "#{acc}#{separator}#{word}" } }
ips.report('each_with_object') { words.each_with_object(+'') { |word, acc| acc << separator << word } }
ips.compare!
end
Warming up --------------------------------------
map & join 910.000 i/100ms
join & prepend 2.882k i/100ms
prepend & join 2.819k i/100ms
reduce 213.000 i/100ms
each_with_object 1.349k i/100ms
Calculating -------------------------------------
map & join 11.818k (± 6.5%) i/s - 59.150k in 5.029508s
join & prepend 26.100k (± 6.8%) i/s - 132.572k in 5.104441s
prepend & join 26.037k (± 6.2%) i/s - 129.674k in 5.000904s
reduce 2.492k (±13.7%) i/s - 12.354k in 5.057033s
each_with_object 12.878k (± 4.8%) i/s - 64.752k in 5.039571s
Comparison:
join & prepend: 26099.7 i/s
prepend & join: 26036.6 i/s - same-ish: difference falls within error
each_with_object: 12878.4 i/s - 2.03x slower
map & join: 11818.4 i/s - 2.21x slower
reduce: 2492.4 i/s - 10.47x slower
Warming up --------------------------------------
map & join 691.000 i/100ms
join & prepend 1.490k i/100ms
prepend & join 1.486k i/100ms
reduce 190.000 i/100ms
each_with_object 747.000 i/100ms
Calculating -------------------------------------
map & join 7.080k (± 2.2%) i/s - 35.932k in 5.077814s
join & prepend 15.571k (± 2.8%) i/s - 78.970k in 5.075875s
prepend & join 15.153k (± 2.3%) i/s - 75.786k in 5.004352s
reduce 1.917k (± 2.5%) i/s - 9.690k in 5.057762s
each_with_object 7.519k (± 2.0%) i/s - 38.097k in 5.068544s
Comparison:
join & prepend: 15570.7 i/s
prepend & join: 15152.8 i/s - same-ish: difference falls within error
each_with_object: 7519.5 i/s - 2.07x slower
map & join: 7079.8 i/s - 2.20x slower
reduce: 1917.1 i/s - 8.12x slower
Warming up --------------------------------------
map & join 425.000 i/100ms
join & prepend 196.000 i/100ms
prepend & join 289.000 i/100ms
reduce 276.000 i/100ms
each_with_object 306.000 i/100ms
Calculating -------------------------------------
map & join 5.988k (±44.2%) i/s - 22.100k in 5.020160s
join & prepend 4.799k (±38.0%) i/s - 17.444k in 4.999857s
prepend & join 4.688k (±35.4%) i/s - 19.652k in 5.061994s
reduce 3.824k (±36.1%) i/s - 16.008k in 5.074519s
each_with_object 4.727k (±36.3%) i/s - 19.278k in 5.019630s
Comparison:
map & join: 5988.3 i/s
join & prepend: 4798.9 i/s - same-ish: difference falls within error
each_with_object: 4727.3 i/s - same-ish: difference falls within error
prepend & join: 4688.4 i/s - same-ish: difference falls within error
reduce: 3824.0 i/s - same-ish: difference falls within error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment