Skip to content

Instantly share code, notes, and snippets.

@gabetax
Created February 10, 2016 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabetax/0d7d6a425930fcb60714 to your computer and use it in GitHub Desktop.
Save gabetax/0d7d6a425930fcb60714 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment