使用< chrono>是否相同+ chrono :: system_clock :: now()vs< time.h> +时钟()? [英] Is the same to use <chrono> + chrono::system_clock::now() vs <time.h> + clock()?

查看:103
本文介绍了使用< chrono>是否相同+ chrono :: system_clock :: now()vs< time.h> +时钟()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了两者并且具有相同的准确度。



两者在VS中的大小相同:8个字节。

所以看起来好像两者都具有相同的准确度。



使用high_resolution_clock可以使用微秒部分。



所以尝试使用high_resolution_clock可能更好,但如果出现错误,那么使用更简单的旧方法。



我尝试过的方法:



I used both and had the same accuracy.

Both have the same size in VS: 8 bytes.
So it seems that both have the same accuracy.

Using the high_resolution_clock it is possible to use the microseconds part.

So perhaps is better to try using the high_resolution_clock but if errors appeared then use the old method that is easier.

What I have tried:

cout<<"Size of std::chrono::time_point<std::chrono::system_clock> "<<sizeof(std::chrono::time_point<std::chrono::system_clock>)<<endl;
cout<<"Size of time_t                                             "<<sizeof(time_t)<<endl;

推荐答案

它不一样,因为 std :: system_clock 是一个实时时钟, clock_t 返回 clock()是基于CPU时间的,称为单调或稳定时钟。



单调时钟仅以固定速率递增,而实时时钟也可以通过与另一个时钟进行比较来间隔调整(设置为不同的值)。 br />


类型的位大小并不能告诉您有关分辨率或准确度的任何信息。这尤其适用于时间类型,即使类型相同的位大小和单位由于使用的时钟实现而具有不同的精度。



如果你想测量和比较时间跨度,你应该使用单调时钟。对于短时间跨度,使用高分辨率时钟。但请注意,在某些系统上,这可能与 clock_t 具有相同的分辨率。可用的分辨率取决于操作系统和硬件(CPU),并且通常必须测量(函数如 clock_getres()只返回理论上时间值的分辨率最大可实现的分辨率。)
It is not the same because std::system_clock is a real time clock and clock_t returned by clock() is CPU time based which is called monotonic or steady clock.

Monotonic clocks are only incrementing at a fixed rate while real time clocks may be also adjusted (set to a different value) at intervals by comparing with another clock.

The bit size of a type does not tell you anything about the resolution or accuracy. This applies especially to time types where even types of same bit size and unit may have different accuracies due to the used clock implemenation.

If you want to measure and compare time spans, you should use a monotonic clock. For short time spans use the high resolution clock. But note that this might have the same resolution as clock_t on some systems. The available resolution depends on the operating system and the hardware (CPU) and must be usually measured (functions like clock_getres() just return the resolution of the time value which is the theoretically max. achievable resolution).


这篇关于使用&lt; chrono&gt;是否相同+ chrono :: system_clock :: now()vs&lt; time.h&gt; +时钟()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆