测量 Windows C++ 的时间、毫秒或微秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23615776/
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
Measure time, milliseconds or microseconds for Windows C++
提问by user3617694
How do you measure the execution time in milliseconds or microseconds in Windows C++?
在 Windows C++ 中,如何以毫秒或微秒为单位测量执行时间?
I found many method one calling time(NULL), but it measures time in seconds only and the seconds clock() (clock_t) measure CPU time, not the actual time.
我发现了许多方法一调用 time(NULL),但它仅以秒为单位测量时间,而秒 clock() (clock_t) 测量 CPU 时间,而不是实际时间。
I found the function gettimeofday(Calendar time) mentioned in this paper: dropbox.com/s/k0zv8pck7ydbakz/1_7-PDF_thesis_2.pdf
我找到了这篇论文中提到的函数gettimeofday(Calendar time):dropbox.com/s/k0zv8pck7ydbakz/1_7-PDF_thesis_2.pdf
This function is for Linux (compute time in milli and microseconds) and not Windows.
此函数适用于 Linux(以毫秒和微秒为单位计算时间)而不是 Windows。
I found an alternative to it for Windows: dropbox.com/s/ofo99b166l7e2gf/gettimeofday.txt
我找到了 Windows 的替代方法:dropbox.com/s/ofo99b166l7e2gf/gettimeofday.txt
And this may be relevant: stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-code-snippet-in-c
这可能是相关的:stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-code-snippet-in-c
回答by bames53
You can use the standard C++ <chrono>
library:
您可以使用标准 C++<chrono>
库:
#include <iostream>
#include <chrono>
// long operation to time
long long fib(long long n) {
if (n < 2) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
int main() {
auto start_time = std::chrono::high_resolution_clock::now();
long long input = 32;
long long result = fib(input);
auto end_time = std::chrono::high_resolution_clock::now();
auto time = end_time - start_time;
std::cout << "result = " << result << '\n';
std::cout << "fib(" << input << ") took " <<
time/std::chrono::milliseconds(1) << "ms to run.\n";
}
One thing to keep in mind is that using <chrono>
enables type safe, generic timing code but to get that benefit you have use it a bit differently than you would use dumb, type-unsafe timing libraries that store durations and time points in types like int
. Here's an answer that explains some specific usage scenarios and the differences between using untyped libraries and best practices for using chrono: https://stackoverflow.com/a/15839862/365496
要记住的一件事是,使用<chrono>
启用类型安全的通用计时代码,但要获得这种好处,您使用它的方式与使用愚蠢的、类型不安全的计时库略有不同,后者将持续时间和时间点存储在int
. 这是一个答案,解释了一些特定的使用场景以及使用无类型库和使用 chrono 的最佳实践之间的区别:https: //stackoverflow.com/a/15839862/365496
The maintainer of Visual Studio's standard library implementation has indicatedthat the low resolution of high_resolution_clock
has been fixed in VS2015 via the use of QueryPerformanceCounter()
.
Visual Studio 标准库实现的维护者表示,high_resolution_clock
VS2015 中已通过使用QueryPerformanceCounter()
.
回答by Darrin Cullop
You need to use the QPC/QPF APIs to get compute the execution time. Invoke the code you want to between calls to QueryPerformanceCounterand then use QueryPerformanceFrequencyto convert it from cycles to microseconds.
您需要使用 QPC/QPF API 来计算执行时间。在对QueryPerformanceCounter 的调用之间调用您想要的代码,然后使用QueryPerformanceFrequency将其从周期转换为微秒。
LARGE_INTEGER nStartTime;
LARGE_INTEGER nStopTime;
LARGE_INTEGER nElapsed;
LARGE_INTEGER nFrequency;
::QueryPerformanceFrequency(&nFrequency);
::QueryPerformanceCounter(&nStartTime);
SomethingToBeTimed();
::QueryPerformanceCounter(&nStopTime);
nElapsed.QuadPart = (nStopTime.QuadPart - nStartTime.QuadPart) * 1000000;
nElapsed.QuadPart /= nFrequency.QuadPart;
References: http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx
参考资料:http: //msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx
回答by MooseBoys
You're looking for QueryPerformanceCounter
and related functions.
您正在寻找QueryPerformanceCounter
和相关的功能。