Skip to content

Instantly share code, notes, and snippets.

@nicolas-brousse
Created April 11, 2024 12:41
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 nicolas-brousse/d0c45ad7286a7b79fd0d8ead187c4d91 to your computer and use it in GitHub Desktop.
Save nicolas-brousse/d0c45ad7286a7b79fd0d8ead187c4d91 to your computer and use it in GitHub Desktop.
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-darwin21]
Warming up --------------------------------------
       concatenation   449.658k i/100ms
          array join   249.847k i/100ms
Calculating -------------------------------------
       concatenation      4.375M (± 2.5%) i/s -     22.033M in   5.039031s
          array join      2.508M (± 4.3%) i/s -     12.742M in   5.090572s

Comparison:
       concatenation:  4375307.9 i/s
          array join:  2508357.4 i/s - 1.74x  slower
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "benchmark-ips"
end
first_name = "John"
last_name = "Doe"
Benchmark.ips do |x|
x.report("concatenation") { "#{first_name} #{last_name}" }
x.report("array join") { [first_name, last_name].join(" ") }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment