<i id='IWYEB'><tr id='IWYEB'><dt id='IWYEB'><q id='IWYEB'><span id='IWYEB'><b id='IWYEB'><form id='IWYEB'><ins id='IWYEB'></ins><ul id='IWYEB'></ul><sub id='IWYEB'></sub></form><legend id='IWYEB'></legend><bdo id='IWYEB'><pre id='IWYEB'><center id='IWYEB'></center></pre></bdo></b><th id='IWYEB'></th></span></q></dt></tr></i><div id='IWYEB'><tfoot id='IWYEB'></tfoot><dl id='IWYEB'><fieldset id='IWYEB'></fieldset></dl></div>

    1. <tfoot id='IWYEB'></tfoot>
      • <bdo id='IWYEB'></bdo><ul id='IWYEB'></ul>

      1. <legend id='IWYEB'><style id='IWYEB'><dir id='IWYEB'><q id='IWYEB'></q></dir></style></legend>

        <small id='IWYEB'></small><noframes id='IWYEB'>

        C++ 在 Linux 上获取毫秒时间——clock() 似乎无法正常工作

        C++ obtaining milliseconds time on Linux -- clock() doesn#39;t seem to work properly(C++ 在 Linux 上获取毫秒时间——clock() 似乎无法正常工作)
        <legend id='UUzgi'><style id='UUzgi'><dir id='UUzgi'><q id='UUzgi'></q></dir></style></legend>
          <tbody id='UUzgi'></tbody>

        <small id='UUzgi'></small><noframes id='UUzgi'>

        • <bdo id='UUzgi'></bdo><ul id='UUzgi'></ul>

          • <i id='UUzgi'><tr id='UUzgi'><dt id='UUzgi'><q id='UUzgi'><span id='UUzgi'><b id='UUzgi'><form id='UUzgi'><ins id='UUzgi'></ins><ul id='UUzgi'></ul><sub id='UUzgi'></sub></form><legend id='UUzgi'></legend><bdo id='UUzgi'><pre id='UUzgi'><center id='UUzgi'></center></pre></bdo></b><th id='UUzgi'></th></span></q></dt></tr></i><div id='UUzgi'><tfoot id='UUzgi'></tfoot><dl id='UUzgi'><fieldset id='UUzgi'></fieldset></dl></div>

                <tfoot id='UUzgi'></tfoot>
                  本文介绍了C++ 在 Linux 上获取毫秒时间——clock() 似乎无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Windows 上,clock() 以毫秒为单位返回时间,但在我正在研究的这个 Linux 机器上,它会将其四舍五入到最接近的 1000,因此精度仅为秒"级别而不是毫秒级别.

                  On Windows, clock() returns the time in milliseconds, but on this Linux box I'm working on, it rounds it to the nearest 1000 so the precision is only to the "second" level and not to the milliseconds level.

                  我找到了一个使用 QTime 类的 Qt 解决方案,实例化一个对象并在其上调用 start() 然后调用 elapsed()获取经过的毫秒数.

                  I found a solution with Qt using the QTime class, instantiating an object and calling start() on it then calling elapsed() to get the number of milliseconds elapsed.

                  我有点幸运,因为我开始使用 Qt,但我想要一个不依赖第三方库的解决方案,

                  I got kind of lucky because I'm working with Qt to begin with, but I'd like a solution that doesn't rely on third party libraries,

                  没有标准的方法可以做到这一点吗?

                  Is there no standard way to do this?

                  更新

                  请不要推荐 Boost ..

                  Please don't recommend Boost ..

                  如果 Boost 和 Qt 可以做到,那肯定不是魔术,他们使用的一定是标准的东西!

                  If Boost and Qt can do it, surely it's not magic, there must be something standard that they're using!

                  推荐答案

                  您可以在方法的开头和结尾使用 gettimeofday,然后区分这两个返回结构.您将获得如下结构:

                  You could use gettimeofday at the start and end of your method and then difference the two return structs. You'll get a structure like the following:

                  struct timeval {
                    time_t tv_sec;
                    suseconds_t tv_usec;
                  }
                  

                  正如下面的两条评论所暗示的那样,clock_gettime(CLOCK_MONOTONIC) 是一个更好的选择,如果你有它,现在应该几乎无处不在.

                  As the two comments below suggest, clock_gettime(CLOCK_MONOTONIC) is a much better choice if you have it available, which should be almost everywhere these days.

                  其他人评论说您也可以将现代 C++ 与 std::chrono::high_resolution_clock 一起使用,但这并不能保证是单调的.改用steady_clock.

                  Someone else commented that you can also use modern C++ with std::chrono::high_resolution_clock, but that isn't guaranteed to be monotonic. Use steady_clock instead.

                  这篇关于C++ 在 Linux 上获取毫秒时间——clock() 似乎无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                      <bdo id='iZ7zf'></bdo><ul id='iZ7zf'></ul>

                    • <small id='iZ7zf'></small><noframes id='iZ7zf'>

                        <i id='iZ7zf'><tr id='iZ7zf'><dt id='iZ7zf'><q id='iZ7zf'><span id='iZ7zf'><b id='iZ7zf'><form id='iZ7zf'><ins id='iZ7zf'></ins><ul id='iZ7zf'></ul><sub id='iZ7zf'></sub></form><legend id='iZ7zf'></legend><bdo id='iZ7zf'><pre id='iZ7zf'><center id='iZ7zf'></center></pre></bdo></b><th id='iZ7zf'></th></span></q></dt></tr></i><div id='iZ7zf'><tfoot id='iZ7zf'></tfoot><dl id='iZ7zf'><fieldset id='iZ7zf'></fieldset></dl></div>
                            <tbody id='iZ7zf'></tbody>
                          <legend id='iZ7zf'><style id='iZ7zf'><dir id='iZ7zf'><q id='iZ7zf'></q></dir></style></legend>

                          <tfoot id='iZ7zf'></tfoot>