#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.

Benchmarking

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.