I am measuring the amount of time printf and cout are for a given output (a number), and want to know if there is a better way of measuring nanosecond precision in C++ (or if this code needs improving).
int rand1 = rand();
auto start = std::chrono::high_resolution_clock::now();
printf("%d", rand1);
auto finish = std::chrono::high_resolution_clock::now();
double secondsPrintf = (double)std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count();
auto start1 = std::chrono::high_resolution_clock::now();
std::cout << rand1;
auto finish1 = std::chrono::high_resolution_clock::now();
std::cout << endl; // measuring performance of printing the number, no need for endl (drastically lowers performance)
double secondsCout = (double)std::chrono::duration_cast<std::chrono::nanoseconds>(finish1-start1).count();
I heard chrono's now() function is very precise, but is there a better way (or more efficient, quicker, etc.) to do any of these operations?