Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple benchmarker
def simple_benchmarker
benchmarks = []
activity = ->(name) { benchmarks << [name, Time.now] }
activity[:started]
yield activity
activity[:finished]
display_benchmarks benchmarks
rescue Exception => e
activity[:error_occurred]
display_benchmarks benchmarks
raise e
end
def display_benchmarks(benchmarks)
first_time, previous_time = nil, nil
benchmarks.each do |act, time|
first_time ||= time
previous_time ||= time
puts "%20s \t+%4f seconds \t (+%4f seconds total) \t%s" % [ act.to_s[0..20], time - previous_time, time - first_time, time]
previous_time = time
end
end
# Usage example
# this line is just so that you can paste the whole file into IRB and it will Just Work(tm)
def make_the_tea ; sleep 3 ; end ; def take_out_the_rubbish ; sleep 3 ; end ; def do_the_dishes ; sleep 3 ; end ; def watch_tv ; sleep 3 ; end
simple_benchmarker do |activity|
make_the_tea
activity[:finished_making_tea]
take_out_the_rubbish
do_the_dishes
activity[:done_boring_chores]
watch_tv
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.