Skip to content

Instantly share code, notes, and snippets.

@evanphx
Created April 10, 2011 23:24
Show Gist options
  • Select an option

  • Save evanphx/912835 to your computer and use it in GitHub Desktop.

Select an option

Save evanphx/912835 to your computer and use it in GitHub Desktop.

Revisions

  1. evanphx created this gist Apr 10, 2011.
    11 changes: 11 additions & 0 deletions 2. ruby 1.8
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    kendall :: git/rbx » ruby scratch/bm_either.rb
    Rehearsal ---------------------------------------------
    includes? 4.410000 0.000000 4.410000 ( 4.421223)
    either? 6.450000 0.000000 6.450000 ( 6.478906)
    compare 2.040000 0.010000 2.050000 ( 2.044516)
    ----------------------------------- total: 12.910000sec

    user system total real
    includes? 4.470000 0.000000 4.470000 ( 4.479386)
    either? 6.520000 0.000000 6.520000 ( 6.542424)
    compare 2.050000 0.010000 2.060000 ( 2.061919)
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    require 'benchmark'

    class Object
    def either?(*args)
    args.include? self
    end
    end

    count = 10000000

    Benchmark.bmbm do |x|

    x.report "includes?" do
    count.times do
    [:one, :two].include?(:one)
    end
    end

    x.report "either?" do
    count.times do
    :one.either?(:one, :two)
    end
    end

    x.report "compare" do
    count.times do
    :one == :one or :one == :two
    end
    end
    end