Created
August 17, 2011 01:20
-
-
Save krukid/1150589 to your computer and use it in GitHub Desktop.
simple timer with dynamic average
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Timer | |
attr_reader :diff, :sum, :step | |
def initialize | |
reset! | |
end | |
def mark! | |
new_mark = now | |
if @mark | |
@sum += @diff | |
@diff = new_mark - @mark | |
@step += 1 | |
end | |
@mark = new_mark | |
end | |
def avg | |
unless @step.zero? | |
(@sum + @diff) / @step | |
else | |
0.0 | |
end | |
end | |
def reset! | |
@mark = nil | |
@step = 0 | |
@diff = 0.0 | |
@sum = 0.0 | |
end | |
private | |
def now | |
Time.now.to_f | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment