Module: TRuby::QuickBenchmark

Defined in:
lib/t_ruby/benchmark.rb

Overview

Quick benchmark helper

Class Method Summary collapse

Class Method Details

.compare(name, &block) ⇒ Object



583
584
585
586
587
588
589
590
# File 'lib/t_ruby/benchmark.rb', line 583

def self.compare(name, &block)
  before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = block.call
  after = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  puts "#{name}: #{((after - before) * 1000).round(3)}ms"
  result
end

.measure(name = "Operation", iterations: 100) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/t_ruby/benchmark.rb', line 568

def self.measure(name = "Operation", iterations: 100)
  times = []

  iterations.times do
    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    yield
    times << Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
  end

  avg = times.sum / times.length
  puts "#{name}: #{(avg * 1000).round(3)}ms avg (#{iterations} iterations)"

  avg
end