Skip to content

Instantly share code, notes, and snippets.

@knightq
Created February 28, 2014 09:50
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 knightq/9268258 to your computer and use it in GitHub Desktop.
Save knightq/9268258 to your computer and use it in GitHub Desktop.
Profile utils
# Usage:
# profile_memory do
# # your_code_to_be_profiled_here...
# end
module ProfileUtils
def get_current_memory_usage
`ps -o rss= -p #{Process.pid}`.to_i
end
def profile_memory(&block)
before = get_current_memory_usage
file, line, _ = caller[0].split(':')
if block_given?
instance_eval(&block)
puts "[#{file}:#{line}: #{(get_current_memory_usage - before) / 1024} MB (consumed)]"
else
before = 0
puts "[#{file}:#{line}: #{(get_current_memory_usage - before) / 1024} MB (all)]"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment