Benchmark
module Benchmark
The Benchmark module provides methods to measure and report the time used to execute Ruby code.
-
Measure the time to construct the string given by the expression
"a"*1_000_000_000
:require 'benchmark' puts Benchmark.measure { "a"*1_000_000_000 }
On my machine (OSX 10.8.3 on i5 1.7 GHz) this generates:
0.350000 0.400000 0.750000 ( 0.835234)
This report shows the user CPU time, system CPU time, the sum of the user and system CPU times, and the elapsed real time. The unit of time is seconds.
-
Do some experiments sequentially using the bm method:
require 'benchmark' n = 5000000 Benchmark.bm do |x| x.report { for i in 1..n; a