Last active
September 15, 2016 21:04
-
-
Save BrianMehrman/6e0adb23d1db5fa0013813a4592d7c66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
module ExtraMath | |
def self.build_math(attr_name) | |
attr_name = attr_name.to_s | |
class_eval %Q{ | |
def #{attr_name}(x) | |
x + x | |
end | |
} | |
end | |
build_math(:do_mathes) | |
define_method "do_maths" do |x| | |
x + x | |
end | |
def do_math(x) | |
x + x | |
end | |
end | |
class TestMath | |
include ExtraMath | |
end | |
t = TestMath.new | |
Benchmark.ips do |x| | |
# Configure the number of seconds used during | |
# the warmup phase (default 2) and calculation phase (default 5) | |
x.config(:time => 5, :warmup => 2) | |
# These parameters can also be configured this way | |
x.time = 5 | |
x.warmup = 2 | |
x.report("normal maths") do | |
t.do_math(2) | |
end | |
x.report("define_method maths") do | |
t.do_maths(2) | |
end | |
x.report("class_eval maths") do | |
t.do_mathes(2) | |
end | |
x.compare! | |
end |
Author
BrianMehrman
commented
Sep 2, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment