#27 Benchmarking and Profiling (tests/test_opts.rb)

Here we’ll talk about two distinct ways to test the execution speed of code. Benchmarking measures the overall speed of the code, while profiling gives more detailed information about how long different parts of the code take to execute, relative to each other.


The previous variants all showed ways to modify the base code in the hopes of making it faster. Here’s where we test our assumptions and find out what really makes a difference. I store it in a directory called tests, meaning that I run it with ruby -w tests/test_opts.rb.

The Code

 #!/usr/bin/env ruby # test_opts.rb =begin comment Run this without warnings to avoid messages about method redefinition, which we are doing intentionally ...

Get Ruby by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.