Skip to content

Instantly share code, notes, and snippets.

@eregon
Created August 1, 2020 14:27
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/561c09e0156a5530f5a100d3e2351c4b to your computer and use it in GitHub Desktop.
Save eregon/561c09e0156a5530f5a100d3e2351c4b 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 read
@c || @d || @e || @f || @g || @h
end
end
# not initialized
class B
def initialize
# nothing
end
def read
@c || @d || @e || @f || @g || @h
end
end
# later initialized
class C
def initialize
# nothing
end
def read
@c || @d || @e || @f || @g || @h
end
def init
@c = @d = @e = @f = @g = @h = nil
end
end
AI = A.new
BI = B.new
CI = C.new
CI.read
CI.init
Benchmark.ips do |x|
r = nil
x.iterations = 2
x.report('initialized read') { r = AI.read }
x.report('uninitialized read') { r = BI.read }
x.report('init + uninit read') { r = CI.read }
x.compare!
end
$ ruby -v
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
$ ruby bench_ivar_set2.rb
Warming up --------------------------------------
initialized read 1.500M i/100ms
uninitialized read 1.259M i/100ms
init + uninit read 1.500M i/100ms
initialized read 1.565M i/100ms
uninitialized read 1.274M i/100ms
init + uninit read 1.488M i/100ms
Calculating -------------------------------------
initialized read 15.292M (± 2.6%) i/s - 76.663M in 5.016528s
uninitialized read 12.763M (± 2.0%) i/s - 64.995M in 5.094516s
init + uninit read 14.833M (± 0.5%) i/s - 74.382M in 5.014795s
initialized read 14.921M (± 2.0%) i/s - 75.098M in 5.035051s
uninitialized read 12.886M (± 1.1%) i/s - 64.995M in 5.044628s
init + uninit read 14.989M (± 2.0%) i/s - 75.870M in 5.063616s
Comparison:
init + uninit read: 14989101.7 i/s
initialized read: 14921107.5 i/s - same-ish: difference falls within error
uninitialized read: 12885730.1 i/s - 1.16x (± 0.00) slower
$ ruby -v
truffleruby 20.3.0-dev-b7a9b466, like ruby 2.6.6, GraalVM CE Native [x86_64-linux]
$ ruby bench_ivar_set2.rb
Warming up --------------------------------------
initialized read 71.509M i/100ms
uninitialized read 71.547M i/100ms
init + uninit read 21.706M i/100ms
initialized read 71.512M i/100ms
uninitialized read 71.595M i/100ms
init + uninit read 21.546M i/100ms
Calculating -------------------------------------
initialized read 709.399M (± 1.6%) i/s - 3.576B in 5.041755s
uninitialized read 704.610M (± 0.8%) i/s - 3.580B in 5.080767s
init + uninit read 212.582M (± 2.2%) i/s - 1.077B in 5.070387s
initialized read 700.674M (± 0.5%) i/s - 3.504B in 5.001153s
uninitialized read 704.396M (± 0.3%) i/s - 3.580B in 5.082056s
init + uninit read 214.761M (± 0.5%) i/s - 1.077B in 5.016355s
Comparison:
uninitialized read: 704396153.8 i/s
initialized read: 700673745.0 i/s - same-ish: difference falls within error
init + uninit read: 214761238.7 i/s - 3.28x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment