Cleaned up the average timing benchmark.

This commit is contained in:
Yorick Peterse 2014-05-01 13:43:33 +02:00
parent 203aea6b1a
commit e54d77fc2f
2 changed files with 27 additions and 17 deletions

View File

@ -20,3 +20,27 @@ end
def read_html
return File.read(File.expand_path('../fixtures/gist.html', __FILE__))
end
##
# Benchmarks the average runtime of the given block.
#
# @param [Fixnum] amount The amount of times to call the block.
# @param [Fixnum] precision
#
def measure_average(amount = 10, precision = 3)
timings = []
amount.times do |iter|
timing = Benchmark.measure { yield }.real
timings << timing.real
puts "Iteration: #{iter + 1}: #{timing.real.round(precision)}"
end
average = timings.inject(:+) / timings.length
puts
puts "Iterations: #{amount}"
puts "Average: #{average.round(precision)} sec"
end

View File

@ -1,21 +1,7 @@
require_relative '../benchmark_helper'
xml = read_big_xml
amount = 10
timings = []
amount.times do |i|
timing = Benchmark.measure do
measure_average do
Oga::XML::Lexer.new(xml).advance { }
end
puts "Iteration #{i + 1}: #{timing.real.round(3)}"
timings << timing.real
end
average = timings.inject(:+) / amount
puts
puts "Iterations: #{amount}"
puts "Average: #{average.round(3)} sec"