Class: Benchmark::ComparisonPartial

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

Instance Method Summary collapse

Constructor Details

#initialize(block, options) ⇒ ComparisonPartial

Returns a new instance of ComparisonPartial.

Parameters:

  • options (Hash)

    @see Benchmark.compare_realtime


4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/better-benchmark/comparison-partial.rb', line 4

def initialize( block, options )
  @block1 = block
  @options = options

  @options[:iterations] ||= 20
  @options[:inner_iterations] ||= 1
  @options[:warmup_iterations] ||= 0

  if @options[:iterations] > 30
    warn "The number of iterations is set to #{@options[:iterations]}.  " +
    "Using too many iterations may make the test results less reliable.  " +
    "It is recommended to increase the number of :inner_iterations instead."
  end
end

Instance Method Details

#with(&block2) ⇒ Object Also known as: to


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/better-benchmark/comparison-partial.rb', line 19

def with( &block2 )
  times1 = []
  times2 = []

  (1..@options[:iterations]).each do |iteration|
    if @options[:verbose]
      $stdout.print "."; $stdout.flush
    end

    @options[:warmup_iterations].times do |i|
      @block1.call( iteration )
    end
    times1 << Benchmark.realtime do
      @options[:inner_iterations].times do |i|
        @block1.call( iteration )
      end
    end

    @options[:warmup_iterations].times do |i|
      block2.call( iteration )
    end
    times2 << Benchmark.realtime do
      @options[:inner_iterations].times do |i|
        block2.call( iteration )
      end
    end
  end

  ::Benchmark.compare_times( times1, times2, @options[:required_significance] )
end