Skip to content

Instantly share code, notes, and snippets.

@movitto
Created October 21, 2019 19:24
Show Gist options
  • Save movitto/a7df9f3f45fb1f95d69905fb6a35be2d to your computer and use it in GitHub Desktop.
Save movitto/a7df9f3f45fb1f95d69905fb6a35be2d to your computer and use it in GitHub Desktop.
Simple Ruby Benchmark Class
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