首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++11模拟C# StopWatch

c++11模拟C# StopWatch
EN

Stack Overflow用户
提问于 2014-05-07 19:24:42
回答 1查看 3.4K关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-07 19:59:20

这里有一个示例,希望演示创建秒表类所需做的所有事情。那堂课我就交给你了。

代码语言:javascript
复制
#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';
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23526556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档