Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

How to use Benchmark Method in Ruby

Benchmark is an important method in Ruby. It is used to measure and report the time used to execute a Ruby code. To use benchmark in Ruby, you can use benchmark.measure {} method.

Benchmark {} Method in Ruby

benchmark {} function exists in Standard Benchmark Library of Ruby. The purpose of this function is to show the total time elapsed in executing a Ruby code. For example:

Output:
User CPU Time
0.547000
System CPU Time
0.000000
Total of Both
0.547000
Total Real Time
(0.537319)

NOTE: In the above code, it shows the user CPU time, system CPU time, the total of both of the times and the real executing time for a given Ruby code. The above result is in seconds.
Mostly, total real time is the essential one. It tells us how long it really took to execute the code.

 

Syntax of benchmark {} in Ruby

The syntax of benchmark {} function in Ruby is benchmark.measure {}

 

Return Value of Benchmark { } in Ruby

Benchmark { } function will return the report in which it shows the total time (in seconds) used to execute a Ruby code.

 

How to get the Executing Time of a Code in Ruby?


Output:
Time taken is 0.300159877.

The sleep method is used to interrupt the current thread for some amount of time. Have a look at the simple way to measure the real time that the function will take to execute a code:

 

Get the Executing Time for a Block of Code In Ruby


Output:

0.047000 0.000000 0.047000 (0.035083)

NOTE: The measuring method provides you the time elapsed to execute the block of code. It already exists in the Ruby standard library.

 

Measure Method of Benchmark Class in Ruby


Output:

0.000000 0.000000 0.000000 ( 0.005182)

 

Use of Benchmark bm Method in Ruby

A simple interface to the benchmark method, bm generates subsequent reports with labels and labels parameters have the similar meaning as for benchmark.

 

See the Example Below for bm Method in Ruby

Output:

Output:

UserSystemtotalreal
0.3280000.0000000.328000(  0.338789)
0.3280000.0000000.328000(  0.331994)
0.3440000.0000000.344000(  0.339183)

NOTE: If you want to put a label to each of the above lines you can do it as shown in the below example:


Output:

LabelUserSystemtotalreal
first_line:0.3280000.0000000.328000( 0.338789)
second_line:0.3280000.0000000.328000( 0.331994)
third_line:0.3440000.0000000.344000( 0.339183)

You request the Benchmark #bm method passing a block. It is a special object produced by the Benchmark. It provides you a reporting method of code that you want to measure. It then runs both of the blocks and prints the result alongside each other.

Note: If the block returns an array of Benchmark :: Tms objects, this will be used to format additional lines of output. If labels parameters are given, these are used to label these extra lines.

 

Use of Benchmark bmbm Method in Ruby

The Benchmark #bm is really the core of Benchmark, but one more method which is Benchmark #bmbm. It is the same method name with bm twice.

Occasionally, with a benchmark, that generates much stuff and due to this, results start to twist because of exchanges with Ruby’s memory distribution or trash collection routines. During generating objects one block needs to run with the trash collections while other do not and sometimes one block get stuck in it.

And when benchmark starts producing uneven results this is the time to use Benchmark #bmbm. The use of bmbm twice actually benchmarks the code twice. At First, it runs the code as a rehearsal and secondly for the real. This is how the system fully initialized and benchmarks work fairly. For example:

Output:


Rehearsal -----------------------------------------
sort! 0.063000 0.000000 0.063000 ( 0.066066)
sort 0.062000 0.000000 0.062000 ( 0.061603)
-------------------------------- total: 0.125000sec

user system total real
sort! 0.063000 0.000000 0.063000 ( 0.064283)
sort 0.062000 0.000000 0.062000 ( 0.061686)

 

Use of benchmark-ips Method in Ruby

The benchmark-ips gem offers even so many other features also to use this install the ips gem in ruby.

Output:


Calculating -------------------------------------
dp: 5.600k i/100ms
recursive: 1.000 i/100ms
-------------------------------------------------
dp: 60.299k (± 2.0%) i/s - 302.400k
recursive: 0.517 (± 0.0%) i/s - 3.000 in 5.800686s

Comparison:
dp: : 60299.5 i/s
recursive: : 0.5 i/s – 116590.15x sloyour

Loading...
LearnAndLearn.com © 2018 - All rights reserved