Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example of each_with_index iterator creating extra objects
GC.disable
before = ObjectSpace.count_objects
Array.new(10000).each do |i|
[0,1].each_with_index do |j, index|
end
end
after = ObjectSpace.count_objects
puts "# of arrays: %d" % (after[:T_ARRAY] - before[:T_ARRAY])
puts "# of extra Ruby objects: %d" % (after[:T_NODE] - before[:T_NODE])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment