Skip to content

Instantly share code, notes, and snippets.

@mwitek
Created April 1, 2013 04:17
Show Gist options
  • Save mwitek/5283222 to your computer and use it in GitHub Desktop.
Save mwitek/5283222 to your computer and use it in GitHub Desktop.
Benchmark test for passing a method to another vs just using an array and each
require 'benchmark'
repetitions = 1000000
def assert_method(string)
string.kind_of?(String)
end
def assert_form_with_fields(assertion)
send(assertion, "blue")
send(assertion, "green")
send(assertion, "orange")
send(assertion, "red")
end
Benchmark.bm(20) do |x|
x.report "Passing method to method" do
repetitions.times do
assert_form_with_fields("assert_method")
end
end
x.report "using each" do
repetitions.times do
colors = ["blue","green","orange","red"]
colors.each {|color| assert_method color }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment