Skip to content

Instantly share code, notes, and snippets.

@eregon
Created August 1, 2020 13:51
Show Gist options
  • Save eregon/282070e6f6686740a0c8e41243fb598b to your computer and use it in GitHub Desktop.
Save eregon/282070e6f6686740a0c8e41243fb598b to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
# initialized
class A
def initialize
@c = @d = @e = @f = @g = @h = nil
end
def b
@c || @d || @e || @f || @g || @h
end
end
# not initialized
class B
def initialize
# nothing
end
def b
@c || @d || @e || @f || @g || @h
end
end
N = 10 # 1000
eval "def a0; #{"A.new;"*10} end"
eval "def b0; #{"B.new;"*10} end"
Benchmark.ips do |x|
x.iterations = 2
x.report('initialized') { a0 }
x.report('uninitialized') { b0 }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment