Skip to content

Instantly share code, notes, and snippets.

@mdespuits
Created April 3, 2013 20:44

Revisions

  1. Matt Bridges created this gist Apr 3, 2013.
    34 changes: 34 additions & 0 deletions performance.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    require 'benchmark'

    ITERATIONS = 10_000

    Benchmark.bm do |bm|
    bm.report("#<< -> #flatten!: ") do
    array = [1,2,3]
    ITERATIONS.times do
    array << [4,5,6]
    array.flatten!
    end
    end

    bm.report("+=: ") do
    array = [1,2,3]
    ITERATIONS.times do
    array += [4,5,6]
    end
    end

    bm.report("#concat: ") do
    array = [1,2,3]
    ITERATIONS.times do
    array.concat [4,5,6]
    end
    end
    end

    # OUTCOME
    #
    # => user system total real
    # => #<< -> #flatten!: 8.280000 0.200000 8.480000 ( 8.473539)
    # => +=: 0.240000 0.350000 0.590000 ( 0.599704)
    # => #concat: 0.000000 0.000000 0.000000 ( 0.001990)