Skip to content

Array#concat vs Array#+ #165

@hvenables

Description

@hvenables
require "benchmark/ips" RANGE = (0..100_000) def fast array = [] RANGE.each { |number| array.concat([number]) } end def slow array = [] RANGE.each { |number| array += [number] } end Benchmark.ips do |x| x.report("Array#concat") { fast } x.report("Array#+") { slow } x.compare! end 
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18] Warming up -------------------------------------- Array#concat 5.000 i/100ms Array#+ 1.000 i/100ms Calculating ------------------------------------- Array#concat 62.530 (±11.2%) i/s - 310.000 in 5.037308s Array#+ 0.194 (± 0.0%) i/s - 1.000 in 5.155399s Comparison: Array#concat: 62.5 i/s Array#+: 0.2 i/s - 322.37x slower 

Happy to make a PR if you want this added

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions