Book Image

Learning Boost C++ Libraries

By : Arindam Mukherjee
Book Image

Learning Boost C++ Libraries

By: Arindam Mukherjee

Overview of this book

Table of Contents (19 chapters)
Learning Boost C++ Libraries
About the Author
About the Reviewers

Measuring program performance using Boost Timer

As programmers, we often need to measure performance of a section of code. While there are several excellent profiling tools available for this purpose, sometimes, being able to instrument our own code is both simple and more precise. The Boost Timer library provides an easy-to-use, portable interface for measuring the execution times and reporting them by instrumenting your code. It is a separately compiled library, not header-only, and internally uses Boost Chrono.


The boost::timer::cpu_timer class is used to measure the execution time of a section of code. In the following example, we write a function that reads the contents of a file and returns it in a dynamic array wrapped in a unique_ptr (see Chapter 3, Memory Management and Exception Safety). It also calculates and prints the time taken to read the file using cpu_timer.

Listing 8.10: Using cpu_timer

 1 #include <fstream>
 2 #include <memory>
 3 #include <boost...