我正在寻找具有微秒精度的 StopWatch 类。我想必须可以实现 using std::chrono::high_resolution_clock,你能建议一些实现吗?
下面是一个示例,希望能够演示创建秒表类所需的所有操作。我会把那个班级的创作留给你。
#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';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3119 次 |
| 最近记录: |