Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- def measure(gc: false, times: 1000, &block)
- GC.start
- GC.disable if gc
- gc_key = if RUBY_VERSION > 2.2
- :total_allocated_objects
- else
- :total_allocated_object
- end
- memory_before = `ps -o rss= -p #{Process.pid}`.to_i
- objects_before = GC.stat(gc_key)
- runtime = Benchmark.realtime do
- times.times do
- yield
- end
- end
- object_delta = GC.stat(gc_key) - objects_before
- memory_delta = (`ps -o rss= -p #{Process.pid}`.to_i) - memory_before
- puts "Iterations: #{times}"
- puts "Runtime: #{runtime} :: #{runtime / times} per call"
- puts "Objects: #{object_delta} :: #{object_delta / times} per call"
- puts "Memory (RSS): #{memory_delta}KB"
- GC.enable
- GC.start
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement