Skip to content

Instantly share code, notes, and snippets.

@eregon
Created August 3, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eregon/f279901e3df450d7a1970b76b9653c71 to your computer and use it in GitHub Desktop.
Save eregon/f279901e3df450d7a1970b76b9653c71 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
def a0; A.new.b; end
def b0; B.new.b; 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