O'Reilly logo

Ruby by Example by Kevin C. Baird

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required