In this example, we will benchmark the difference between reading the contents of a file using std::fstream and reading them using mmap().
It should be noted that the mmap() function leverages a system call to directly map a file into the program, and we expect mmap() to be faster than the C++ APIs highlighted in this chapter. This is because the C++ APIs have to perform an additional memory copy, which is obviously slower.
We will start this example by defining the size of the file we plan to read, as follows:
constexpr auto size = 0x1000;
Next, we must define a benchmark function to record how long it takes to perform an action:
template<typename FUNC>...
auto benchmark(FUNC func) {
auto stime = std::chrono::high_resolution_clock::now();
func();
auto etime = std::chrono::high_resolution_clock::now();
return etime - stime;
}