我正在寻找微秒精度的StopWatch类。我想使用std::chrono::high_resolution_clock实现一定是可能的,您能建议一些实现吗?
发布于 2014-05-07 19:59:20
这里有一个示例,希望演示创建秒表类所需做的所有事情。那堂课我就交给你了。
#include <iostream>
#include <chrono>
int main()
{
// save some typing
namespace cr = std::chrono;
// you can replace this with steady_clock or system_clock
typedef cr::high_resolution_clock my_clock;
// get the clock time before operation.
// note that this is a static function, and
// we don't actually create a clock object
auto start_time = my_clock::now();
// perform some operation
std::cin.ignore();
// get the clock time after the operation
auto end_time = my_clock::now();
// get the elapsed time
auto diff = end_time - start_time;
// convert from the clock rate to a millisecond clock
auto milliseconds = cr::duration_cast<cr::milliseconds>(diff);
// get the clock count (i.e. the number of milliseconds)
auto millisecond_count = milliseconds.count();
std::cout << millisecond_count << '\n';
}https://stackoverflow.com/questions/23526556
复制相似问题