Skip to content

Instantly share code, notes, and snippets.

@heycarsten
Created December 17, 2008 02:57
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 heycarsten/36922 to your computer and use it in GitHub Desktop.
Save heycarsten/36922 to your computer and use it in GitHub Desktop.
Rehearsal ----------------------------------------------------------------
Case Equality Operator 3.590000 0.970000 4.560000 ( 4.560098)
Object#is_a? Method 3.520000 0.970000 4.490000 ( 4.485853)
Object#class == class 4.230000 0.970000 5.200000 ( 5.212452)
Case Statement (class.to_s) 3.390000 0.970000 4.360000 ( 4.366338)
Case Statement (Proper Form) 2.340000 0.960000 3.300000 ( 3.293166)
------------------------------------------------------ total: 21.910000sec
user system total real
Case Equality Operator 3.570000 0.960000 4.530000 ( 4.541271)
Object#is_a? Method 3.490000 0.970000 4.460000 ( 4.465701)
Object#class == class 4.230000 0.990000 5.220000 ( 5.400060)
Case Statement (class.to_s) 3.460000 1.030000 4.490000 ( 4.961087)
Case Statement (Proper Form) 2.340000 1.000000 3.340000 ( 3.477076)
Nothing too surprising, but interesting to know.
require 'benchmark'
test_var = 'Curiosity Killed The Cat'
Benchmark.bmbm do |bm|
bm.report('Case Equality Operator') do
1_000_000.times do
true if String === test_var
true if Integer === test_var
true if Hash === test_var
true if Array === test_var
true if Float === test_var
true if Bignum === test_var
end
end
bm.report('Object#is_a? Method') do
1_000_000.times do
true if test_var.is_a?(String)
true if test_var.is_a?(Integer)
true if test_var.is_a?(Hash)
true if test_var.is_a?(Array)
true if test_var.is_a?(Float)
true if test_var.is_a?(Bignum)
end
end
bm.report('Object#class == class') do
1_000_000.times do
true if test_var.class == String
true if test_var.class == Integer
true if test_var.class == Hash
true if test_var.class == Array
true if test_var.class == Float
true if test_var.class == Bignum
end
end
bm.report('Case Statement (class.to_s)') do
1_000_000.times do
case test_var.class.to_s
when 'String' : true
when 'Integer' : true
when 'Hash' : true
when 'Array' : true
when 'Float' : true
when 'Bignum' : true
end
end
end
bm.report('Case Statement (Proper Form)') do
1_000_000.times do
case test_var
when String : true
when Integer : true
when Hash : true
when Array : true
when Float : true
when Bignum : true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment