我需要计算一个函数的执行时间。
目前,我使用time.h
在功能开始时:
time_t tbegin,tend;
double texec=0.000;
time(&tbegin);
返回之前:
time(&tend);
texec = difftime(tend,tbegin);
它运行正常,但以texec为整数给出结果。
如何以毫秒为单位执行执行时间?
答案 0 :(得分:5)
大多数简单程序的计算时间以毫秒为单位。所以,我想,你会觉得这很有用。
{{1}}
如果你想计算整个程序的运行时并且你在Unix系统上,那么使用time命令运行你的程序,就像这次./a.out
答案 1 :(得分:3)
您可以在C ++ 14中使用带有auto
参数的lambda来计时其他功能。您可以将定时函数的参数传递给lambda。我这样做:
// Timing in C++14 with auto lambda parameters
#include <iostream>
#include <chrono>
// need C++14 for auto lambda parameters
auto timing = [](auto && F, auto && ... params)
{
auto start = std::chrono::steady_clock::now();
std::forward<decltype(F)>(F)
(std::forward<decltype(params)>(params)...); // execute the function
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start).count();
};
void f(std::size_t numsteps) // we'll measure how long this function runs
{
// need volatile, otherwise the compiler optimizes the loop
for (volatile std::size_t i = 0; i < numsteps; ++i);
}
int main()
{
auto taken = timing(f, 500'000'000); // measure the time taken to run f()
std::cout << "Took " << taken << " milliseconds" << std::endl;
taken = timing(f, 100'000'000); // measure again
std::cout << "Took " << taken << " milliseconds" << std::endl;
}
优点是您可以将任何可调用对象传递给timing
lambda。如果你不能使用C ++ 14 auto
lambda参数,那么你需要编写一个模板仿函数来模拟&#34;模拟&#34; lambda。
但如果你想保持简单,你可以这样做:
auto start = std::chrono::steady_clock::now();
your_function_call_here();
auto end = std::chrono::steady_clock::now();
auto taken = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << taken << " milliseconds";
如果您知道自己不会在运行期间更改系统时间,则可以使用std::chrono::high_resolution_clock
代替,这可能更精确。但是,std::chrono::steady_clock
对运行期间的系统时间更改不敏感。
答案 2 :(得分:2)
您可以创建一个像source这样的函数:
typedef unsigned long long timestamp_t;
static timestamp_t
timestampinmilliseconf ()
{
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}
然后你可以用它来获得时差。
timestamp_t time1 = get_timestamp();
// Your function
timestamp_t time2 = get_timestamp();
对于Windows,您可以使用此功能:
#ifdef WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#include <ctime>
#endif
typedef long long int64; typedef unsigned long long uint64;
/* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
* windows and linux. */
int64 GetTimeMs64()
{
#ifdef WIN32
/* Windows */
FILETIME ft;
LARGE_INTEGER li;
/* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
* to a LARGE_INTEGER structure. */
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
uint64 ret = li.QuadPart;
ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */
return ret;
#else
/* Linux */
struct timeval tv;
gettimeofday(&tv, NULL);
uint64 ret = tv.tv_usec;
/* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
ret /= 1000;
/* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
ret += (tv.tv_sec * 1000);
return ret;
#endif
}
答案 3 :(得分:0)
<chrono>
中的有一个
班std::chrono::high_resolution_clock
那就是你想要的。虽然它有点参与;
#include <chrono>
using namespace std;
using namespace chrono;
auto t1 = high_resolution_clock::now();
// do calculation here
auto t2 = high_resolution_clock::now();
auto diff = duration_cast<duration<double>>(t2 - t1);
// now elapsed time, in seconds, as a double can be found in diff.count()
long ms = (long)(1000*diff.count());