更有效的方法来测量printf和std :: cout的纳秒性能/速度[C ++]

时间:2013-05-19 03:43:03

标签: c++ chrono

我正在测量给定输出(数字)的printf和cout的时间量,并且想知道在C ++中是否有更好的方法来测量纳秒精度(或者如果此代码需要改进)。

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();

我听说chrono的now()函数非常精确,但有没有更好的方法(或者更高效,更快速等等)来执行这些操作?

0 个答案:

没有答案