Class: Benchmark::Comparer

Inherits:
Object
  • Object
show all
Defined in:
lib/better-benchmark/comparer.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Comparer

Expected column layout of the CSV files is: <identifier of thing tested>,<time in fractional seconds>

Parameters:

  • argv (Array)

    The command line arguments passed to the bencher script



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/better-benchmark/comparer.rb', line 13

def initialize( argv )
  @iterations = 10
  @executable = 'ruby'

  while argv.any?
    arg = argv.shift
    case arg
    when '-p'
      @max_p = argv.shift
    else
      if @file1.nil?
        @file1 = arg
      elsif @file2.nil?
        @file2 = arg
      end
    end
  end

  if @file1.nil? || @file2.nil?
    print_usage
    exit 2
  end

  @timings_before = Hash.new { |h,k| h[k] = Array.new }
  @timings_after = Hash.new { |h,k| h[k] = Array.new }
end

Instance Method Details



5
6
7
# File 'lib/better-benchmark/comparer.rb', line 5

def print_usage
  puts "bbench-compare [-p <max p-value>] <timings1.csv> <timings2.csv>"
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/better-benchmark/comparer.rb', line 40

def run
  CSV.foreach(@file1) do |row|
    @timings_before[ row[0] ] << row[1].to_f
  end
  CSV.foreach(@file2) do |row|
    @timings_after[ row[0] ] << row[1].to_f
  end

  run_results = Hash.new
  @timings_before.each_key do |thing_tested|
    results = Benchmark.compare_times( @timings_before[thing_tested], @timings_after[thing_tested], @max_p )
    improvement = ( results[:results2][:mean] - results[:results1][:mean] ) / results[:results1][:mean]

    run_results[thing_tested] = { improvement: improvement, significant: results[:significant] }
    puts( "%s\t%+.1f%%\t%s" % [thing_tested, improvement * 100.0, results[:significant] ? '' : '*' ] )
  end

  run_results
end