Skip to content

Instantly share code, notes, and snippets.

@floere
Created May 9, 2010 21:18
Show Gist options
  • Save floere/395419 to your computer and use it in GitHub Desktop.
Save floere/395419 to your computer and use it in GitHub Desktop.
# From http://eigenclass.org/R2/writings/object-size-ruby-ocaml:
#
# Ruby 1.9 stores up to 3 instance variables in
# the object slot without using an external table,
# so an object with one IV will only take 5 words.
# Beyond 3 instance variables, it reverts to an
# external IV array which is resized exponentially
# (factor 1.25) as new elements are added.
#
# Run with 1.9.
#
require 'benchmark'
class Zero
end
class One
def initialize
@one = 1
end
end
class Two
def initialize
@one = 1
@two = 2
end
end
class Three
def initialize
@one = 1
@two = 2
@three = 3
end
end
class Four
def initialize
@one = 1
@two = 2
@three = 3
@four = 4
end
end
class Five
def initialize
@one = 1
@two = 2
@three = 3
@four = 4
@five = 5
end
end
class Six
def initialize
@one = 1
@two = 2
@three = 3
@four = 4
@five = 5
@six = 6
end
end
class Seven
def initialize
@one = 1
@two = 2
@three = 3
@four = 4
@five = 5
@six = 6
@seven = 7
end
end
puts Benchmark.measure {
100_000.times do
Zero.new
end
}
puts Benchmark.measure {
100_000.times do
One.new
end
}
puts Benchmark.measure {
100_000.times do
Two.new
end
}
puts Benchmark.measure {
100_000.times do
Three.new
end
}
puts Benchmark.measure {
100_000.times do
Four.new
end
}
puts Benchmark.measure {
100_000.times do
Five.new
end
}
puts Benchmark.measure {
100_000.times do
Six.new
end
}
puts Benchmark.measure {
100_000.times do
Seven.new
end
}
@kschiess
Copy link

  user     system      total        real
Twenty  0.230000   0.000000   0.230000 (  0.233681)
   Ten  0.130000   0.000000   0.130000 (  0.130510)
  Nine  0.130000   0.000000   0.130000 (  0.128657)
 Eight  0.100000   0.000000   0.100000 (  0.103342)
 Seven  0.100000   0.000000   0.100000 (  0.094256)
   Six  0.070000   0.000000   0.070000 (  0.076708)
  Five  0.070000   0.000000   0.070000 (  0.064305)
  Four  0.060000   0.000000   0.060000 (  0.063965)
 Three  0.050000   0.000000   0.050000 (  0.044275)
   Two  0.040000   0.000000   0.040000 (  0.038634)
   One  0.030000   0.000000   0.030000 (  0.035694)
  Zero  0.030000   0.000000   0.030000 (  0.027715) 

ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0]

Looks roughly O(n) to me.. perhaps not in memory, but was that your point?

@floere
Copy link
Author

floere commented May 10, 2010

The point is the almost significant increase at 4 ivars. My Ruby though (ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-darwin9.8.0]) is relatively old and wrinkled.


  0.050000   0.000000   0.050000 (  0.053586)
  0.060000   0.000000   0.060000 (  0.062729)
  0.060000   0.000000   0.060000 (  0.069838)
  0.070000   0.000000   0.070000 (  0.072942)
  0.100000   0.000000   0.100000 (  0.107794) <-- 4 ivars
  0.110000   0.000000   0.110000 (  0.112962)
  0.130000   0.000000   0.130000 (  0.129338)
  0.130000   0.000000   0.130000 (  0.135911)

@floere
Copy link
Author

floere commented May 10, 2010

P.S: I was investigating whether the moment where Ruby needs to revert to an external IV array is visible in performance tests. I'd say it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment