lib/rgot/b.rb in rgot-0.0.4 vs lib/rgot/b.rb in rgot-0.0.5

- old
+ new

@@ -1,18 +1,17 @@ module Rgot class B < Common attr_accessor :n - def initialize(benchmark_module, name, opts) + def initialize(benchmark_module, name, opts={}) super() @n = 1 @module = benchmark_module @name = name @opts = opts - @benchtime = @opts.fetch(:benchtime, 1).to_f @timer_on = false @duration = 0 - @module.extend @module + @module.extend @module if @module end def start_timer if !@timer_on @start = Rgot.now @@ -32,45 +31,63 @@ @start = Rgot.now end @duration = 0 end - def run + def run(&block) n = 1 - run_n(n) - while !failed? && @duration < @benchtime && @n < 1e9 - if @duration < (@benchtime / 100.0) - n *= 100 - elsif @duration < (@benchtime / 10.0) - n *= 10 - elsif @duration < (@benchtime / 5.0) - n *= 5 - elsif @duration < (@benchtime / 2.0) - n *= 2 + benchtime = @opts.fetch(:benchtime, 1).to_f + run_n(n.to_i, block) + while !failed? && @duration < benchtime && @n < 1e9 + if @duration < (benchtime / 100.0) + @n *= 100 + elsif @duration < (benchtime / 10.0) + @n *= 10 + elsif @duration < (benchtime / 5.0) + @n *= 5 + elsif @duration < (benchtime / 2.0) + @n *= 2 else - n *= 1.2 + if @n.to_i == 1 + break + end + @n *= 1.2 end - run_n(n) + run_n(@n.to_i, block) end + + BenchmarkResult.new(n: @n, t: @duration) end - def report - printf("%s\t%d\t%.3f ns/op\n", @name, @n, @duration / @n * 1_000_000_000) + def run_parallel + raise LocalJumpError, "no block given" unless block_given? + + @opts[:procs].times do + fork { + Array.new(@opts[:threads]) { + Thread.new { + yield PB.new(bn: @n) + }.tap { |t| t.abort_on_exception = true } + }.each(&:join) + } + end + @n *= @opts[:procs] * @opts[:threads] + Process.waitall end private - def run_n(n) + def run_n(n, block=nil) GC.start i = 0 @n = n reset_timer start_timer - call + if block + block.call(self) + else + @module.instance_method(@name).bind(@module).call(self) + end stop_timer - end - - def call - @module.instance_method(@name).bind(@module).call(self) end end end