Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created October 25, 2009 14:40
Show Gist options
  • Save djberg96/218090 to your computer and use it in GitHub Desktop.
Save djberg96/218090 to your computer and use it in GitHub Desktop.
# Adaptive benchmarking code I copied from the blog of Mauricio Fernandez
module Adaptive
class Benchmark
# The version of the adaptive-benchmark library
VERSION = '0.1.0'
# Default options
REPORT_OPT = {:precision => 0.1, :confidence => 0.95}
def initialize(field_length, minimum_iterations)
@field_length = field_length
@minimum_iterations = minimum_iterations
end
def self.bm(field_length = 10, minimum_iterations = 10)
puts "#{" " * field_length}\t\t\tstddev\t\truns\tinterval\tconfidence"
yield new(field_length, minimum_iterations)
end
def report(name, options = {})
old_sync, $stdout.sync = $stdout.sync, true
opt = REPORT_OPT.clone.update(options)
sample_avg = sample_variance = 0
tms_to_total = lambda{ |tms| tms.utime + tms.stime }
take_sample = lambda do |i|
GC.start
t0 = tms_to_total[Process.times]
yield
exec_time = tms_to_total[Process.times] - t0
new_sample_avg = sample_avg + (exec_time - sample_avg) / i
if i == 1
sample_avg = new_sample_avg
next
end
sample_variance = (1 - 1.0/i) * sample_variance + (i+1) * (new_sample_avg - sample_avg) ** 2
sample_avg = new_sample_avg
end
(1..@minimum_iterations).each{ |i| take_sample[i] }
population_variance = 1.0 * @minimum_iterations / (@min_runs - 1) * sample_variance
a = opt[:precision] * sample_avg
num_runs = (sample_variance / (a ** 2 * (1 - opt[:confidence]))).ceil
total_runs = @minimum_iterations + num_runs
(@minimum_iterations + 1..total_runs + 1).each{ |i| take_sample[i] }
population_variance = 1.0 * total_runs / (total_runs - 1) * sample_variance
puts "%-#{@field_length}s %8.6f\t%8.6f\t%-6d\t%8.6f\t%3d%%" %
[name, sample_avg, Math.sqrt(population_variance), total_runs, a, opt[:confidence] * 100]
ensure
$stdout.sync = old_sync
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment