Skip to content

Instantly share code, notes, and snippets.

@urbanautomaton
Last active December 10, 2015 12:48
Show Gist options
  • Save urbanautomaton/4436640 to your computer and use it in GitHub Desktop.
Save urbanautomaton/4436640 to your computer and use it in GitHub Desktop.
Attempt to separate out method call cost in DCI benchmark
require 'rubygems'
require 'delegate'
require 'benchmark'
include Benchmark
class ExampleClass
def foo; 42; end
end
class OtherClass
def bar; 3; end
end
module ExampleMixin
def foo; 43; end
end
def create_objects
@extended = ExampleClass.new
@unmodified = OtherClass.new
end
def apply_mixin
@extended.extend(ExampleMixin)
end
n = 5000000
Benchmark.benchmark(CAPTION, 17, FORMAT, "> meth unmod:", "> meth local:", "> meth non-local:") do |bm|
objects = bm.report("object creation:") do
n.times do
create_objects
end
end
full_setup = bm.report("creation + extend:") do
n.times do
create_objects
apply_mixin
end
end
no_mixin = bm.report("no mixin:") do
n.times do
create_objects
@extended.bar
end
end
mixin_local = bm.report("local modified:") do
n.times do
create_objects
apply_mixin
@extended.foo
end
end
mixin_non_local = bm.report("non-local:") do
n.times do
create_objects
apply_mixin
@unmodified.bar
end
end
[no_mixin - objects, mixin_local - full_setup, mixin_non_local - full_setup]
end
# ruby 1.9.3-p194
$ ruby dci.rb
user system total real
object creation: 2.540000 0.020000 2.560000 ( 2.551338)
creation + extend: 11.900000 0.020000 11.920000 ( 11.925966)
no mixin: 2.860000 0.000000 2.860000 ( 2.866278)
local modified: 12.580000 0.010000 12.590000 ( 12.591549)
non-local: 12.620000 0.020000 12.640000 ( 12.645279)
> meth unmod: 0.320000 -0.020000 0.300000 ( 0.314940)
> meth local: 0.680000 -0.010000 0.670000 ( 0.665583)
> meth non-local: 0.720000 0.000000 0.720000 ( 0.719313)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment