在C++中计算经过的时间
calculating time elapsed in C++
我需要计算函数运行的时间。现在我正在使用 std::clock,据我所知,这是在测量 CPU 时间,这可能与实时不同。
std::clock_t start;
double duration;
start = std::clock();
someFunctionToMeasure();
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
所以我想知道两件事
std::clock 究竟是如何工作的?它在计算该函数时是否只是测量 CPU?
是否有更好的方法来测量计算我的函数所用的时间?
使用 <chrono>
,您需要的代码可能如下所示:
using clock = std::chrono::system_clock;
using sec = std::chrono::duration<double>;
// for milliseconds, use using ms = std::chrono::duration<double, std::milli>;
const auto before = clock::now();
someFunctionToMeasure();
const sec duration = clock::now() - before;
std::cout << "It took " << duration.count() << "s" << std::endl;
注意:感谢霍华德对上述内容的有益评论。
如果您多次需要此代码段并且 start/end 大约是您调用范围的入口点和出口点 someFunctionToMeasure()
,将其包装到实用程序中可能是有意义的 class 在构造函数和析构函数中对 now()
进行两次调用。
来源:
http://en.cppreference.com/w/cpp/chrono/c/clock
时钟只记录在 process the clock is executing on. So your sample code is keeping track of how much CPU time it took for your function to execute in. This is notably different from keeping track of real time, because the process your function is running on could be preempted 上经过的时间,而 cpu 可以在您的函数等待完成时执行其他代码一段时间.
回答你的第二个问题可能有助于澄清你所说的 "better" 的意思。听起来您想跟踪函数执行的时间量,据我所知,这段代码完成了该任务。如果您想实时跟踪时间量,其他答案会给出相关示例。
只想引入现代方法来使用 <chrono>
和 C++17 中方便的 std::invoke
对任何可调用对象进行计时。适用于成员、lambda 或自由函数,或任何其他可调用对象。
// Just for convenience
using Seconds = std::chrono::duration<double>;
// Measure how much time the given function takes to execute using chrono
// Pass the function name, then all relevant arguments, including the object as the first if it's a member function
template<typename Function, typename... Args>
Seconds measure(Function&& toTime, Args&&... a)
{
auto start{std::chrono::steady_clock::now()}; // Start timer
std::invoke(std::forward<Function>(toTime), std::forward<Args>(a)...); // Forward and call
auto stop{std::chrono::steady_clock::now()}; // Stop timer
return (stop - start);
}
这将 return 函数执行所花费的时间。如果您还需要 return 值,您可以使用 Seconds
和 return 值创建 std::pair
,因为 std::invoke
将正确地 return 什么可调用 returns.
那么你可以这样使用它:
auto t1 = measure(normalFunction);
auto t2 = measure(&X::memberFunction, obj, 4);
auto t3 = measure(lambda, 2, 3);
分别在自由函数、成员函数和lambda上。
我需要计算函数运行的时间。现在我正在使用 std::clock,据我所知,这是在测量 CPU 时间,这可能与实时不同。
std::clock_t start;
double duration;
start = std::clock();
someFunctionToMeasure();
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
所以我想知道两件事
std::clock 究竟是如何工作的?它在计算该函数时是否只是测量 CPU?
是否有更好的方法来测量计算我的函数所用的时间?
使用 <chrono>
,您需要的代码可能如下所示:
using clock = std::chrono::system_clock;
using sec = std::chrono::duration<double>;
// for milliseconds, use using ms = std::chrono::duration<double, std::milli>;
const auto before = clock::now();
someFunctionToMeasure();
const sec duration = clock::now() - before;
std::cout << "It took " << duration.count() << "s" << std::endl;
注意:感谢霍华德对上述内容的有益评论。
如果您多次需要此代码段并且 start/end 大约是您调用范围的入口点和出口点 someFunctionToMeasure()
,将其包装到实用程序中可能是有意义的 class 在构造函数和析构函数中对 now()
进行两次调用。
来源: http://en.cppreference.com/w/cpp/chrono/c/clock
时钟只记录在 process the clock is executing on. So your sample code is keeping track of how much CPU time it took for your function to execute in. This is notably different from keeping track of real time, because the process your function is running on could be preempted 上经过的时间,而 cpu 可以在您的函数等待完成时执行其他代码一段时间.
回答你的第二个问题可能有助于澄清你所说的 "better" 的意思。听起来您想跟踪函数执行的时间量,据我所知,这段代码完成了该任务。如果您想实时跟踪时间量,其他答案会给出相关示例。
只想引入现代方法来使用 <chrono>
和 C++17 中方便的 std::invoke
对任何可调用对象进行计时。适用于成员、lambda 或自由函数,或任何其他可调用对象。
// Just for convenience
using Seconds = std::chrono::duration<double>;
// Measure how much time the given function takes to execute using chrono
// Pass the function name, then all relevant arguments, including the object as the first if it's a member function
template<typename Function, typename... Args>
Seconds measure(Function&& toTime, Args&&... a)
{
auto start{std::chrono::steady_clock::now()}; // Start timer
std::invoke(std::forward<Function>(toTime), std::forward<Args>(a)...); // Forward and call
auto stop{std::chrono::steady_clock::now()}; // Stop timer
return (stop - start);
}
这将 return 函数执行所花费的时间。如果您还需要 return 值,您可以使用 Seconds
和 return 值创建 std::pair
,因为 std::invoke
将正确地 return 什么可调用 returns.
那么你可以这样使用它:
auto t1 = measure(normalFunction);
auto t2 = measure(&X::memberFunction, obj, 4);
auto t3 = measure(lambda, 2, 3);
分别在自由函数、成员函数和lambda上。