Advertisement
lacostenycoder

Ruby Array memory

Dec 6th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.55 KB | None | 0 0
  1. require "memory_profiler"
  2. mem = MemoryProfiler.report do
  3.   array = [*1..1_000_000]
  4. end
  5.  
  6. mem2 = MemoryProfiler.report do
  7.   array = [*1..1_000_000]
  8.   b = array.select {|x| x % 3 == 0}
  9. end
  10.  
  11. mem3 = MemoryProfiler.report do
  12.   array = [*1..1_000_000]
  13.   b = []
  14.   1.upto(1_000_000) {|i| b << i if i % 3 == 0 }
  15. end
  16.  
  17. puts mem.allocated_memory_by_class.first
  18. puts mem2.allocated_memory_by_class.first
  19. puts mem3.allocated_memory_by_class.first
  20.  
  21. #{:data=>"Array", :count=>19636352}
  22. #{:data=>"Array", :count=>27636392}
  23. #{:data=>"Array", :count=>23084168}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement