Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
Forked from evanphx/2. ruby 1.8
Created April 11, 2011 05:34
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 jamesarosen/913105 to your computer and use it in GitHub Desktop.
Save jamesarosen/913105 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Object
def either?(*args)
args.include? self
end
def in?(*args)
case args.length
when 0
false
when 1
self == args[0]
when 2
self == args[0] || self == args[1]
else
args.include?(self)
end
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
1.9.2> ruby benchmark.rb
includes? 1.930000 0.010000 1.940000 ( 1.932690)
either? 2.530000 0.000000 2.530000 ( 2.533304)
unrolled_in? 3.410000 0.000000 3.410000 ( 3.413661)
compare 1.240000 0.000000 1.240000 ( 1.236956)
--------------------------------------- total: 9.120000sec
user system total real
includes? 1.910000 0.010000 1.920000 ( 1.907518)
either? 2.530000 0.000000 2.530000 ( 2.525952)
unrolled_in? 3.390000 0.000000 3.390000 ( 3.395632)
compare 1.240000 0.000000 1.240000 ( 1.238029)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment