Method: Benchmark.measure

Defined in:
lib/benchmark.rb

.measure(label = "") ⇒ Object

Returns the time used to execute the given block as a Benchmark::Tms object. Takes label option.

require 'benchmark'

n = 1000000

time = Benchmark.measure do
  n.times { a = "1" }
end
puts time

Generates:

0.220000   0.000000   0.220000 (  0.227313)


302
303
304
305
306
307
308
309
310
311
312
# File 'lib/benchmark.rb', line 302

def measure(label = "") # :yield:
  t0, r0 = Process.times, Process.clock_gettime(Process::CLOCK_MONOTONIC)
  yield
  t1, r1 = Process.times, Process.clock_gettime(Process::CLOCK_MONOTONIC)
  Benchmark::Tms.new(t1.utime  - t0.utime,
                     t1.stime  - t0.stime,
                     t1.cutime - t0.cutime,
                     t1.cstime - t0.cstime,
                     r1 - r0,
                     label)
end