Skip to content

Instantly share code, notes, and snippets.

@schacon
Created June 22, 2011 15:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schacon/1040423 to your computer and use it in GitHub Desktop.
Save schacon/1040423 to your computer and use it in GitHub Desktop.
simple ruby stopwatch
class Stopwatch
attr_writer :splits, :max, :start, :end, :total
def initialize(message)
@message = message
@splits = []
@max = 5
end
def split(message)
@max = message.size > @max ? message.size : @max
time = Time.now
@start = time if !@start
@end = time
@splits << [time, message]
end
def report
puts
@total = @end - @start
last_time = nil
last_message = nil
@splits.each do |split|
time, message = split
if last_time
elapsed = time - last_time
ptime(last_message, elapsed)
end
last_time = time
last_message = message
end
ptime("Total", @total)
end
def ptime(message, time)
extra = ''
if time < @total
extra = ((time / @total) * 100).to_i.to_s + '%'
end
puts message.rjust(@max + 1) + ' ' + time.to_s.rjust(10) + ' ' + extra
end
end
@schacon
Copy link
Author

schacon commented Jan 23, 2012 via email

Copy link

ghost commented Feb 9, 2015

how can i see the output? i ran your app it shown nothing in the shell.

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