Skip to content

Instantly share code, notes, and snippets.

@jonmagic
Created December 23, 2014 23:11
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 jonmagic/fc32b31ac119afb5dbb4 to your computer and use it in GitHub Desktop.
Save jonmagic/fc32b31ac119afb5dbb4 to your computer and use it in GitHub Desktop.
require "benchmark"
class Foo
def initialize(bar)
@bar = bar
end
attr_reader :bar
def do_case
case bar
when "a"
"a"
when "b"
"b"
when "c"
"c"
when "d"
"d"
end
end
def do_send
send(bar)
end
private
def a
"a"
end
def b
"b"
end
def c
"c"
end
def d
"d"
end
end
foo = Foo.new("a")
puts "benchmarking case: a"
puts Benchmark.measure { 1_000_000.times { foo.do_case } }
puts "benchmarking send: a"
puts Benchmark.measure { 1_000_000.times { foo.do_send } }
foo = Foo.new("d")
puts "benchmarking case: d"
puts Benchmark.measure { 1_000_000.times { foo.do_case } }
puts "benchmarking send: d"
puts Benchmark.measure { 1_000_000.times { foo.do_send } }
~/Projects $ ruby case_vs_send.rb
benchmarking case: a
0.280000 0.000000 0.280000 ( 0.299583)
benchmarking send: a
0.330000 0.010000 0.340000 ( 0.342247)
benchmarking case: d
0.260000 0.000000 0.260000 ( 0.272458)
benchmarking send: d
0.360000 0.010000 0.370000 ( 0.390494)
~/Projects $ ruby case_vs_send.rb
benchmarking case: a
0.290000 0.000000 0.290000 ( 0.307030)
benchmarking send: a
0.350000 0.010000 0.360000 ( 0.377197)
benchmarking case: d
0.240000 0.000000 0.240000 ( 0.257030)
benchmarking send: d
0.350000 0.010000 0.360000 ( 0.385077)
~/Projects $ ruby case_vs_send.rb
benchmarking case: a
0.290000 0.010000 0.300000 ( 0.320443)
benchmarking send: a
0.350000 0.000000 0.350000 ( 0.363823)
benchmarking case: d
0.260000 0.000000 0.260000 ( 0.275449)
benchmarking send: d
0.320000 0.010000 0.330000 ( 0.348568)
~/Projects $ ruby case_vs_send.rb
benchmarking case: a
0.310000 0.010000 0.320000 ( 0.383576)
benchmarking send: a
0.310000 0.000000 0.310000 ( 0.308605)
benchmarking case: d
0.270000 0.000000 0.270000 ( 0.281642)
benchmarking send: d
0.330000 0.010000 0.340000 ( 0.348779)
~/Projects $ ruby case_vs_send.rb
benchmarking case: a
0.280000 0.000000 0.280000 ( 0.295815)
benchmarking send: a
0.360000 0.010000 0.370000 ( 0.392535)
benchmarking case: d
0.280000 0.000000 0.280000 ( 0.290594)
benchmarking send: d
0.340000 0.010000 0.350000 ( 0.363607)
~/Projects $ ruby case_vs_send.rb
benchmarking case: a
0.300000 0.010000 0.310000 ( 0.312629)
benchmarking send: a
0.340000 0.000000 0.340000 ( 0.355788)
benchmarking case: d
0.240000 0.000000 0.240000 ( 0.260041)
benchmarking send: d
0.340000 0.000000 0.340000 ( 0.348781)
~/Projects $ ruby case_vs_send.rb
benchmarking case: a
0.300000 0.000000 0.300000 ( 0.311963)
benchmarking send: a
0.320000 0.010000 0.330000 ( 0.331134)
benchmarking case: d
0.290000 0.000000 0.290000 ( 0.299811)
benchmarking send: d
0.310000 0.000000 0.310000 ( 0.322822)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment