Skip to content

Instantly share code, notes, and snippets.

@jbodah
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save jbodah/a32991626c2715f33b4b to your computer and use it in GitHub Desktop.

Select an option

Save jbodah/a32991626c2715f33b4b to your computer and use it in GitHub Desktop.
measurer
# Measures runtime, memory usage,
# of GC calls
class Measurer
require "json"
require "benchmark"
def self.measure(opts = {}, &block)
use_gc = opts.has_key?(:use_gc) ? opts[:use_gc] : true
if use_gc
# collect memory allocated during library loading
# and our own code before the measurement
GC.start
else
GC.disable
end
memory_before = `ps -o rss= -p #{Process.pid}`.to_i/1024
gc_stat_before = GC.stat
time = Benchmark.realtime do
yield
end
gc_stat_after = GC.stat
GC.start if use_gc
memory_after = `ps -o rss= -p #{Process.pid}`.to_i/1024
GC.enable
puts({
RUBY_VERSION => {
gc: use_gc ? 'enabled' : 'disabled',
time: time.round(2),
gc_count: gc_stat_after[:count] - gc_stat_before[:count], memory: "%dM" % (memory_after - memory_before)
} }.to_json)
end
end
@jbodah

jbodah commented Mar 30, 2015

Copy link
Copy Markdown
Author

@jbodah

jbodah commented Mar 30, 2015

Copy link
Copy Markdown
Author

Usage:

load 'measurer.rb'
require 'csv'
csv = '/Users/Bodah/Downloads/FL_insurance_sample.csv'

Measurer.measure {  CSV.open(csv).readlines.map { |line| line.map { |col| col.downcase.gsub(/\b('?[a-z])/) { $1.capitalize } } }}
=> {"2.1.3":{"gc":"enabled","time":1.69,"gc_count":14,"memory":"152M"}}

Measurer.measure(use_gc: false) {  CSV.open(csv).readlines.map { |line| line.map { |col| col.downcase.gsub(/\b('?[a-z])/) { $1.capitalize } } }}
=> {"2.1.3":{"gc":"disabled","time":1.64,"gc_count":0,"memory":"190M"}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment