Created
May 9, 2010 21:18
-
-
Save floere/395419 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 | |
} |
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)
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
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?