-
-
Save eregon/282070e6f6686740a0c8e41243fb598b 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' | |
# 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