Created
October 21, 2019 19:24
-
-
Save movitto/a7df9f3f45fb1f95d69905fb6a35be2d to your computer and use it in GitHub Desktop.
Simple Ruby Benchmark Class
This file contains hidden or 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
require 'xrbp' | |
class Benchmark | |
def initialize(max=1000) | |
@max = max | |
@snapshots = [] | |
end | |
def capture!(n=1) | |
@snapshots << [n, Time.now] | |
@snapshots.shift until @snapshots.size <= @max | |
end | |
def first | |
@snapshots.first.last | |
end | |
def last | |
@snapshots.last.last | |
end | |
def average | |
total = @snapshots.collect { |s| s.first }.sum | |
duration = last - first | |
total / duration | |
end | |
end # class Benchmark | |
b = Benchmark.new | |
b.capture!(1) | |
sleep(1) | |
b.capture!(2) | |
sleep(1) | |
b.capture!(1) | |
puts b.average |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment