A first example of using Boost

Dirk Eddelbuettel — written Jan 14, 2013 — updated Aug 2, 2022 — source

Boost is, to quote the quote by Sutter and Alexandrescu which adornes the Boost website, …one of the most highly regarded and expertly designed C++ library projects in the world.

The impact of Boost on C++ cannot be overstated. Boost is, at its core, a collection of thoroughly designed and peer-reviewed libraries. Some of these have been included into the new C++11 standard (see our intro post on C++11) as for example lambda functions which we illustrated in another post on C++11.

Boost is mostly implemented using templates. That means headers files only, and compile-time – but not linking. Which is perfect for example posts like these.

Many, many Boost libraries are useful, and we could fill a series of posts. Here, as an introduction, we going to use two simple functions from the Boost.Math library to compute greatest common denominator and least common multiple.

I should note that I initially wrote this post on a machine with Boost in a standard system location. So stuff just works. Others may have had to install Boost from source, and into a non-standard location, which may have required an -I flag, not unlike how we initially added the C++11 flag in this post before the corresponding plugin was added.

These days, and thanks to the newer BH package which, if installed, provides Boost headers for use by R in compilations, it works by just inclusing a [[Rcpp::depends(BH)]] attribute as we do here.

// We can now use the BH package
// [[Rcpp::depends(BH)]]

#include <Rcpp.h>
#include <boost/integer/common_factor.hpp>

using namespace Rcpp;
 
// [[Rcpp::export]]
int computeGCD(int a, int b) {
    return boost::integer::gcd(a, b);
}

// [[Rcpp::export]]
int computeLCM(int a, int b) {
    return boost::integer::lcm(a, b);
}

We can test these:

a <- 6
b <- 15
cat( c(computeGCD(a,b), computeLCM(a,b)), "\n")
3 30 
a <- 96
b <- 484
cat( c(computeGCD(a,b), computeLCM(a,b)), "\n")
4 11616 

And as kindly suggested and submitted by Kohske Takahashi, we can also benchmark this against an R solution using the numbers package:

library(rbenchmark)
library(numbers)

a <- 962
b <- 4842

res <- benchmark(r1 = c(computeGCD(a,b), computeLCM(a,b)),
                 r2 = c(GCD(a,b), LCM(a,b)),
                 replications = 5000)
print(res[,1:4])
  test replications elapsed relative
1   r1         5000   0.034    1.000
2   r2         5000   0.067    1.971

This shows a nice performance gain.

Postscriptum: The post was updated after 9 1/2 years to update the Boost header from the now-deprecated boost/math/common_factor.hpp to the now-preferred boost/integer/common_factor.hpp, and updated http references to https while we were at it.

tags: basics  boost 

Related Articles