Skip to content

Instantly share code, notes, and snippets.

@korun
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save korun/1a9a1a42338cb4edfda1 to your computer and use it in GitHub Desktop.

Select an option

Save korun/1a9a1a42338cb4edfda1 to your computer and use it in GitHub Desktop.

Revisions

  1. korun revised this gist Apr 2, 2015. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    n = 1_000_000

    login = 'login'
    time = Time.now.to_i

    Benchmark.bmbm 20 do |results|
    @@ -13,21 +14,21 @@
    # result: 3 array object usage
    results.report '[] + []' do
    n.times do
    ['login', time] + [3, 4, 5]
    [login, time] + [3, 4, 5]
    end
    end

    # result: 2 array object usage
    results.report '[].push(*[])' do
    n.times do
    ['login', time].push(*[3, 4, 5])
    [login, time].push(*[3, 4, 5])
    end
    end

    # result: 1 array object usage
    results.report '[].unshift(a, b)' do
    n.times do
    [3, 4, 5].unshift('login', time)
    [3, 4, 5].unshift(login, time)
    end
    end
    end
  2. korun created this gist Apr 2, 2015.
    33 changes: 33 additions & 0 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    require 'benchmark'

    n = 1_000_000

    time = Time.now.to_i

    Benchmark.bmbm 20 do |results|
    results.report 'cycle' do
    n.times do
    end
    end

    # result: 3 array object usage
    results.report '[] + []' do
    n.times do
    ['login', time] + [3, 4, 5]
    end
    end

    # result: 2 array object usage
    results.report '[].push(*[])' do
    n.times do
    ['login', time].push(*[3, 4, 5])
    end
    end

    # result: 1 array object usage
    results.report '[].unshift(a, b)' do
    n.times do
    [3, 4, 5].unshift('login', time)
    end
    end
    end