From e54d77fc2f45aef3445e0c3848eaba325169e176 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 1 May 2014 13:43:33 +0200 Subject: [PATCH] Cleaned up the average timing benchmark. --- benchmark/benchmark_helper.rb | 24 ++++++++++++++++++++++++ benchmark/lexer/big_xml_average.rb | 20 +++----------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/benchmark/benchmark_helper.rb b/benchmark/benchmark_helper.rb index 4704afc..0cd2145 100644 --- a/benchmark/benchmark_helper.rb +++ b/benchmark/benchmark_helper.rb @@ -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 diff --git a/benchmark/lexer/big_xml_average.rb b/benchmark/lexer/big_xml_average.rb index c9edbbe..ccb3f2e 100644 --- a/benchmark/lexer/big_xml_average.rb +++ b/benchmark/lexer/big_xml_average.rb @@ -1,21 +1,7 @@ require_relative '../benchmark_helper' -xml = read_big_xml -amount = 10 -timings = [] +xml = read_big_xml -amount.times do |i| - timing = Benchmark.measure do - Oga::XML::Lexer.new(xml).advance { } - end - - puts "Iteration #{i + 1}: #{timing.real.round(3)}" - - timings << timing.real +measure_average do + Oga::XML::Lexer.new(xml).advance { } end - -average = timings.inject(:+) / amount - -puts -puts "Iterations: #{amount}" -puts "Average: #{average.round(3)} sec"