Last active
June 18, 2019 23:42
-
-
Save rmm5t/8eb05273217ec9d85ee90592b2c57876 to your computer and use it in GitHub Desktop.
Benchmarking command object pattern vs PORO
This file contains hidden or 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' | |
require 'active_support/all' | |
class CommandObject | |
def self.execute_with(arg) | |
new(arg).tap(&:run) | |
end | |
def initialize(arg) | |
@arg = arg | |
end | |
def run | |
@ran = true | |
end | |
end | |
class PORO | |
def initialize(arg) | |
@arg = arg | |
@ran = true | |
end | |
end | |
def a_method(arg) | |
@arg = arg | |
@ran = true | |
end | |
n = 100_000 | |
Benchmark.bmbm(15) do |x| | |
x.report("command-object") { n.times { CommandObject.execute_with("thing") } } | |
x.report("PORO") { n.times { PORO.new("thing") } } | |
x.report("method") { n.times { a_method("thing") } } | |
end | |
# >> user system total real | |
# >> command-object 0.028089 0.000126 0.028215 ( 0.028239) | |
# >> PORO 0.016680 0.000005 0.016685 ( 0.016683) | |
# >> method 0.007609 0.000002 0.007611 ( 0.007611) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment