c++11模拟的c#秒表

c++11 analog of C# StopWatch

本文关键字:秒表 模拟 c++11      更新时间:2023-10-16

我正在寻找秒表类与微秒精度。我想使用std::chrono::high_resolution_clock一定是可以实现的,你能建议一些实现吗?

下面的示例有望演示创建Stopwatch类所需的所有操作。我将把创建该类留给您。

#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';
}