C++ 以纳秒为单位获取本地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12937963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get local time in nanoseconds
提问by Narek
Possible Duplicate:
C++ Timer function to provide time in nano seconds
可能的重复:
C++ Timer 函数以纳秒为单位提供时间
I need to measure the duration of a function execution in nanoseconds resolution. Is it possible? Our ordinary computer hardware and software are able to give such a precision for time? If yes how to accomplish that with c++? Is it possible with Boost library?
我需要以纳秒分辨率测量函数执行的持续时间。是否可以?我们普通的计算机硬件和软件能够给出这样的时间精度吗?如果是,如何用 C++ 实现?Boost库可以吗?
回答by bames53
Yes, today most hardware supports this sort of resolution, and the C++ standard library has an API that can support it as well. Unfortunately not all implementations of C++ actually do provide it.
是的,今天大多数硬件都支持这种分辨率,C++ 标准库也有一个 API 可以支持它。不幸的是,并非所有的 C++ 实现都确实提供了它。
The API is the <chrono>
library introduced in C++11:
该 API 是<chrono>
C++11 中引入的库:
#include <iostream>
#include <chrono>
int main() {
auto start = std::chrono::high_resolution_clock::now();
// operation to be timed ...
auto finish = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count() << "ns\n";
}
The <chrono>
implementation in libc++ for Darwin provides nanosecond resolution, but it seems the implementation in VS2012 only goes to tenths of milliseconds. The above code will still give times in terms of nanoseconds, but timings less than 100 microseconds will end up being zero nanoseconds.
在<chrono>
libc中++达尔文的实现提供纳秒级的分辨率,但它似乎在VS2012的实施只到毫秒的十分之一。上面的代码仍然以纳秒为单位给出时间,但小于 100 微秒的计时最终将为零纳秒。
Boost also provides an implemenation, boost::chrono, which does seem to use nanoseconds on Windows. It's also usable with C++03.
Boost 还提供了一个实现 boost::chrono,它似乎在 Windows 上使用了纳秒。它也可用于 C++03。
#include <boost/chrono.hpp>
int main() {
boost::chrono::high_resolution_clock::time_point t1 =
boost::chrono::high_resolution_clock::now();
boost::chrono::high_resolution_clock::time_point t2 =
boost::chrono::high_resolution_clock::now();
std::cout << boost::chrono::duration_cast<boost::chrono::nanoseconds>(t2-t1) << "\n";
// boost chrono has more advanced output, .count() isn't needed, nor is manually writing out the units
}
回答by Jerry Coffin
About the best you can do with the Windows API is QueryPerformanceCounter. You can obtain its resolution with QueryPerformanceFrequency. Depending on the situation, this may be use the CPU's RDTSC instruction, in which case the frequency is basically the clock frequency of the CPU. In other cases, it uses a separate clock with a frequency of 1.024 MHz, so the resolution is basically one microsecond.
您可以使用 Windows API 做的最好的事情是QueryPerformanceCounter。您可以使用QueryPerformanceFrequency获得其分辨率。视情况而定,这可能是使用 CPU 的 RDTSC 指令,在这种情况下,频率基本上是 CPU 的时钟频率。在其他情况下,它使用频率为 1.024 MHz 的单独时钟,因此分辨率基本为 1 微秒。
C++11 adds a chrono
class that may be useful as well -- assuming you're using a compiler that already has it. If your compiler doesn't provide an implementation, Boost Chronois a reasonable substitute that works with most existing compilers/libraries. Again, there's no guarantee that it'll provide nanosecond resolution, but I'd expect that on Windows it'll probably be a portable wrapper around QPC/QPF, so it'll probably give the same resolution they do, just more portably.
C++11 添加了一个chrono
也可能有用的类——假设您使用的编译器已经有了它。如果您的编译器不提供实现,Boost Chrono是一个合理的替代品,可与大多数现有编译器/库配合使用。同样,不能保证它会提供纳秒分辨率,但我希望在 Windows 上它可能是围绕 QPC/QPF 的便携式包装器,因此它可能会提供与它们相同的分辨率,只是更便携。
回答by mttalxndrgrrtt
I'm not sure if you're looking for clock or difftime, but one of these should be what you're looking for:
我不确定您是在寻找时钟还是 difftime,但其中之一应该是您正在寻找的:
http://www.cplusplus.com/reference/clibrary/ctime/clock/
http://www.cplusplus.com/reference/clibrary/ctime/clock/
http://www.cplusplus.com/reference/clibrary/ctime/difftime/
http://www.cplusplus.com/reference/clibrary/ctime/difftime/
You can use this before and after functions and compare the differences of runtimes to see the overall runtime of a function.
您可以在函数之前和之后使用它并比较运行时的差异以查看函数的整体运行时。
回答by Offirmo
And of course there is also the good old Boost::date_time:
当然还有老式的Boost::date_time:
#include <boost/date_time/posix_time/posix_time.hpp>
boost::posix_time::ptime date_time = boost::posix_time::microsec_clock::universal_time();
microsec only, though.
不过,只有微秒。