简体   繁体   English

C ++中的便携式高精度双时间戳?

[英]Portable good precision double timestamp in C++?

Here's what I'd need to do: 这是我需要做的事情:

double now=getdoubletimestampsomehow();

Where getdoubletimestampsomehow() should be a straight-forward, easy to use function returning a double value representing the number of seconds elapsed from a given date. 其中getdoubletimestampsomehow()应该是一个简单易用的函数,返回一个double值,表示从给定日期开始经过的秒数。 I'd need it to be quite precise, but I don't really need it to be more precise than a few milliseconds. 我需要它非常精确,但我并不需要它比几毫秒更精确。 Portability is quite important, if it isn't possible to directly port it anywhere could you please tell me both an unix and a windows way to do it? 可移植性非常重要,如果无法将其直接移植到任何地方,请告诉我unix和windows方法吗?

Have you looked at Boost and particularly its Date_Time library ? 你看过Boost ,特别是它的Date_Time库吗? Here is the seconds since epoch example. 这是自纪元以来秒数

You will be hard-pressed to find something more portable, and of higher resolution. 您将很难找到更便携,更高分辨率的东西。

C++11 introduced the <chrono> header containing quite a few portable clocks. C ++ 11引入了包含相当多便携式时钟的<chrono>标头。 The highest resolution clock among them is the std::chrono::high_resolution_clock . 其中最高分辨率的时钟是std::chrono::high_resolution_clock

It provides the current time as a std::chrono::time_point object which has a time_since_epoch member. 它将当前时间作为std::chrono::time_point对象提供,该对象具有time_since_epoch成员。 This might contain what you want. 这可能包含您想要的内容。

Reference : 参考

Prior to the release of the C++11 standard, there was no standard way in which one could accurately measure the execution time of a piece of code. 在发布C ++ 11标准之前,没有一种标准方法可以准确地测量一段代码的执行时间。 The programmer was forced to use external libraries like Boost, or routines provided by each operating system. 程序员被迫使用Boost等外部库或每个操作系统提供的例程。

The C++11 chrono header file provides three standard clocks that could be used for timing one's code: C ++ 11 chrono头文件提供了三个标准时钟,可用于计算一个代码:

  • system_clock - this is the real-time clock used by the system; system_clock - 这是系统使用的实时时钟;
  • high_resolution_clock - this is a clock with the shortest tick period possible on the current system; high_resolution_clock - 这是一个在当前系统上具有最短滴答周期的时钟;
  • steady_clock - this is a monotonic clock that is guaranteed to never be adjusted. steady_clock - 这是一个单调的时钟,保证永远不会被调整。

If you want to measure the time taken by a certain piece of code for execution, you should generally use the steady_clock, which is a monotonic clock that is never adjusted by the system. 如果要测量某段代码执行所需的时间,通常应该使用steady_clock,这是一个永远不会被系统调整的单调时钟。 The other two clocks provided by the chrono header can be occasionally adjusted, so the difference between two consecutive time moments, t0 < t1, is not always positive. 由计时器头提供的另外两个时钟可以偶尔调整,因此两个连续时刻之间的差值t0 <t1并不总是正的。

Portable good precision double timestamp in C++? C ++中的便携式高精度双时间戳?

There is no portable way to get high-precision timestamp (milliseconds) without using 3rd party libraries. 没有使用第三方库,没有可移植的方法来获得高精度时间戳(毫秒)。 Maximum precision you'll get is 1 second, using time / localtime / gmtime . 使用time / localtime / gmtime ,您将获得的最大精度为1秒。

If you're fine with 3rd party libraries, use either Boost or Qt 4. 如果你对第三方库很好,可以使用Boost或Qt 4。

both an unix and a windows way to do it? unix和windows方式都可以吗?

GetSystemTime on Windows and gettimeofday on linux. Windows上的GetSystemTime和Linux上的gettimeofday

Please note that if you're planning to use timestamps to determine order of some events, then it might be a bad idea. 请注意,如果您计划使用时间戳来确定某些事件的顺序,那么这可能是一个坏主意。 System clock might have very limited precision (10 milliseconds on windows platform), in which case several operations performed consequently can produce same timestamp. 系统时钟可能具有非常有限的精度(在Windows平台上为10毫秒),在这种情况下,执行的几个操作因此可以产生相同的时间戳。 So, to determine order of events you would need "logical timestamps" ("vector clock" is one of examples). 因此,要确定事件的顺序,您需要“逻辑时间戳” (“矢量时钟”是其中一个示例)。

On windows platform, there are highly precise functions that can be used to determine how much time has passed since some point in the past ( QueryPerformanceCounter ), but they aren't connected to timestamps. 在Windows平台上,有一些高度精确的函数可用于确定自过去的某个点( QueryPerformanceCounter )以来经过了多长时间,但它们没有连接到时间戳。

Doubles are not precise - therefore you idea for double now=getdoubletimestampsomehow(); 双打double now=getdoubletimestampsomehow(); - 因此你想要double now=getdoubletimestampsomehow(); falls down at the first hurdle. 在第一个障碍下跌倒。

Others have mentioned other possibilities. 其他人提到了其他可能性。 I would explore those. 我会探索那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM