测量执行时间

编程入门 行业动态 更新时间:2024-10-04 09:23:18
测量执行时间 - gettimeofday与clock()与chrono的关系(Measuring execution time - gettimeofday versus clock() versus chrono)

我有一个子程序,应该每毫秒执行一次。 我想检查确实发生了什么。 但是我从不同的功能获得不同的执行时间。 我一直试图理解这些功能之间的差异(关于这个主题有几个SO问题),但我无法理解我得到的结果。 请忘记全局变量等。这是一个遗留代码,用C语言编写,移植到C ++,我正在努力改进,所以很麻烦。

< header stuff> std::chrono::high_resolution_clock::time_point tchrono; int64_t tgettime; float tclock; void myfunction(){ <all kinds of calculations> using ms = std::chrono::duration<double, std::milli>; std::chrono::high_resolution_clock::time_point tmpchrono = std::chrono::high_resolution_clock::now(); printf("chrono %f (ms): \n",std::chrono::duration_cast<ms>(tmpchrono-tchrono).count()); tchrono = tmpchrono; struct timeval tv; gettimeofday (&tv, NULL); int64_t tmpgettime = (int64_t) tv.tv_sec * 1000000 + tv.tv_usec; printf("gettimeofday: %lld\n",tmpgettime-tgettime); tgettime = tmpgettime; float tmpclock = 1000.0f*((float)clock())/CLOCKS_PER_SEC; printf("clock %f (ms)\n",tmpclock-tclock); tclock = tmpclock; <more stuff> }

输出是:

chrono 0.998352 (ms): gettimeofday: 999 clock 0.544922 (ms)

为什么不同? 我希望时钟至少和其他时钟一样大,不是吗?

I have a subroutine that should be executed once every milisecond. I wanted to check that indeed that's what's happening. But I get different execution times from different functions. I've been trying to understand the differences between these functions (there are several SO questions about the subject) but I cannot get my head around the results I got. Please forget the global variables etc. This is a legacy code, written in C, ported to C++, which I'm trying to improve, so is messy.

< header stuff> std::chrono::high_resolution_clock::time_point tchrono; int64_t tgettime; float tclock; void myfunction(){ <all kinds of calculations> using ms = std::chrono::duration<double, std::milli>; std::chrono::high_resolution_clock::time_point tmpchrono = std::chrono::high_resolution_clock::now(); printf("chrono %f (ms): \n",std::chrono::duration_cast<ms>(tmpchrono-tchrono).count()); tchrono = tmpchrono; struct timeval tv; gettimeofday (&tv, NULL); int64_t tmpgettime = (int64_t) tv.tv_sec * 1000000 + tv.tv_usec; printf("gettimeofday: %lld\n",tmpgettime-tgettime); tgettime = tmpgettime; float tmpclock = 1000.0f*((float)clock())/CLOCKS_PER_SEC; printf("clock %f (ms)\n",tmpclock-tclock); tclock = tmpclock; <more stuff> }

and the output is:

chrono 0.998352 (ms): gettimeofday: 999 clock 0.544922 (ms)

Why the difference? I'd expect clock to be at least as large as the others, or not?

最满意答案

std :: chrono :: high_resolution_clock :: now()甚至没有工作。

std::chrono::milliseconds表示整数的毫秒数。 转换为该表示时,较高粒度的时间表示将截断为整数毫秒。 然后将其分配给具有double表示和秒比的持续时间。 然后将duration对象 - 而不是double - 传递给printf 。 所有这些步骤都是错误的。

要将毫秒作为浮点数,请执行以下操作:

using ms = std::chrono::duration<double, std::milli>; std::chrono::duration_cast<ms>(tmpchrono-tchrono).count();

clock()返回进程使用的处理器时间。 这将取决于OS调度程序为您的进程提供了多长时间。 除非该过程是系统中唯一的过程,否则这将与通过的挂钟时间不同。

gettimeofday()返回挂钟时间。

使用high_resolution_clock :: now()和gettimeofday()有什么区别?

两者都测量挂钟时间。 两者的内部表示是实现定义的。 两者的粒度也是实现定义的。

gettimeofday是POSIX标准的一部分,因此适用于符合该标准的所有操作系统(POSIX.1-2001)。 gettimeofday不是单调的,即受设置时间(通过ntpd或管理员)和夏令时变化的影响。

high_resolution_clock表示实现提供的具有最小滴答周期的时钟。 它可能是std :: chrono :: system_clock或std :: chrono :: steady_clock的别名,或者是第三个独立时钟。

high_resolution_clock是c ++标准库的一部分,因此可用于符合该标准的所有编译器(c ++ 11)。 high_resolution_clock可能是也可能不是单调的。 这可以使用high_resolution_clock::is_steady进行测试

std::chrono::high_resolution_clock::now() is not even working.

std::chrono::milliseconds represents the milliseconds as integers. When you convert to that representation, time representations of higher granularity are truncated to whole milliseconds. Then you assign it to a duration that has a double representation and seconds-ratio. Then you pass the duration object - instead of a double - to printf. All of those steps are wrong.

To get the milliseconds as a floating point, do this:

using ms = std::chrono::duration<double, std::milli>; std::chrono::duration_cast<ms>(tmpchrono-tchrono).count();

clock() returns the processor time the process has used. That will depend on how much time the OS scheduler has given to your process. Unless the process is the only one on the system, this will be different from the passed wall clock time.

gettimeofday() returns the wall clock time.

What's the difference between using high_resolution_clock::now() and gettimeofday() ?

Both measure the wall clock time. The internal representation of both is implementation defined. The granularity of both is implementation defined as well.

gettimeofday is part of the POSIX standard and therefore available in all operating systems that comply with that standard (POSIX.1-2001). gettimeofday is not monotonic, i.e. it's affected by things like setting the time (by ntpd or by adminstrator) and changes in daylight saving time.

high_resolution_clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.

high_resolution_clock is part of the c++ standard library and therefore available in all compilers that comply with that standard (c++11). high_resolution_clock may or might not be monotonic. This can be tested with high_resolution_clock::is_steady

更多推荐

本文发布于:2023-08-07 13:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464093.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:执行时间   测量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!