In the following code, I repeatedly call std::chrono::high_resolution_clock::now
twice, and measure the time it took between these two calls. I would expect this time to be very small, since there is no other code is run between these two calls. However, I observe strange behavior.
For small N, the max element is within a few nanoseconds, as expected. However, the more I increase N, I get very large outliers, and have gotten up to a few milliseconds. Why does this happen?
In other words, why does the max element of v increase as I increase N in the following code?
#include <iostream>
#include <vector>
#include <chrono>
#include <algorithm>
int main()
{
using ns = std::chrono::nanoseconds;
uint64_t N = 10000000;
std::vector<uint64_t> v(N, 0);
for (uint64_t i = 0; i < N; i++) {
auto start = std::chrono::high_resolution_clock::now();
v[i] = std::chrono::duration_cast<ns>(std::chrono::high_resolution_clock::now() - start).count();
}
std::cout << "max: " << *std::max_element(v.begin(), v.end()) << std::endl;
return 0;
}