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
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
[[Rcpp::depends(BH)]] attribute as we do here.
We can test these:
And as kindly suggested and submitted by Kohske Takahashi, we can also benchmark this against an R solution using the numbers package:
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
to the now-preferred
boost/integer/common_factor.hpp, and updated http
references to https while we were at it.