Created
February 12, 2019 13:17
-
-
Save edisonywh/c61b3ab50359e68454d87b2c13d5d1a8 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
# Note: This is pretty badly written code, I just wrote it as a quick proof for https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg | |
# To get the growth rate of the array, just look at the jump in memory bytes after every copy. You can see that it's about 1.5~ | |
require 'objspace' # Require this for additional ObjectSpace methods | |
def memory_calculator | |
array = Array.new | |
result = {} | |
100.times do |i| | |
array << i | |
memory = ObjectSpace.memsize_of(array) | |
memory_in_bytes = memory / 8.0 | |
result["#{memory_in_bytes} bytes"] = { | |
num_elements: array.size, | |
total_available_memory: "#{memory_in_bytes} bytes", | |
} | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment