如何计算C ++中的时差

在C ++中计算时间差的最好方法是什么? 我计算一个程序的执行速度,所以我对毫秒感兴趣。 更好的是,秒。毫秒

被接受的答案是有效的,但是需要包括ctime或者time.h,正如评论中所指出的那样。

参见std::clock()函数。

 const clock_t begin_time = clock(); // do something std::cout << float( clock () - begin_time ) / CLOCKS_PER_SEC; 

如果你想计算自己的执行时间(不是用户),最好是在时钟滴答(而不是秒)。

编辑:
负责的头文件 – <ctime><time.h>

我会认真考虑使用Boost,特别是boost :: posix_time :: ptime和boost :: posix_time :: time_duration(在http://www.boost.org/doc/libs/1_38_0/doc/html/date_time/posix_time .html )。

它是跨平台的,易于使用,以我的经验提供了操作系统提供的最高级别的时间分辨率。 可能也很重要; 它提供了一些非常好的IO操作符。

要使用它来计算程序执行的差异(微秒,可能矫枉过正),它会看起来像这样[浏览器写入,未经testing]:

 ptime time_start(microsec_clock::local_time()); //... execution goes here ... ptime time_end(microsec_clock::local_time()); time_duration duration(time_end - time_start); cout << duration << '\n'; 

如果你正在使用c ++ 11,这里是一个简单的包装(见这个要点 ):

 #include <iostream> #include <chrono> class Timer { public: Timer() : beg_(clock_::now()) {} void reset() { beg_ = clock_::now(); } double elapsed() const { return std::chrono::duration_cast<second_> (clock_::now() - beg_).count(); } private: typedef std::chrono::high_resolution_clock clock_; typedef std::chrono::duration<double, std::ratio<1> > second_; std::chrono::time_point<clock_> beg_; }; 

或者对于* nix上的c ++ 03:

 #include <iostream> #include <ctime> class Timer { public: Timer() { clock_gettime(CLOCK_REALTIME, &beg_); } double elapsed() { clock_gettime(CLOCK_REALTIME, &end_); return end_.tv_sec - beg_.tv_sec + (end_.tv_nsec - beg_.tv_nsec) / 1000000000.; } void reset() { clock_gettime(CLOCK_REALTIME, &beg_); } private: timespec beg_, end_; }; 

使用示例:

 int main() { Timer tmr; double t = tmr.elapsed(); std::cout << t << std::endl; tmr.reset(); t = tmr.elapsed(); std::cout << t << std::endl; return 0; } 

升压1.46.0及以上包括Chrono库:

thread_clock类提供对真实线程挂钟的访问,即调用线程的实际CPU时钟。 线程相对当前时间可以通过调用thread_clock :: now()来获得

 #include <boost/chrono/thread_clock.hpp> { ... using namespace boost::chrono; thread_clock::time_point start = thread_clock::now(); ... thread_clock::time_point stop = thread_clock::now(); std::cout << "duration: " << duration_cast<milliseconds>(stop - start).count() << " ms\n"; 

在Windows中:使用GetTickCount

 //GetTickCount defintition #include <windows.h> int main() { DWORD dw1 = GetTickCount(); //Do something DWORD dw2 = GetTickCount(); cout<<"Time difference is "<<(dw2-dw1)<<" milliSeconds"<<endl; } 

以防万一你在Unix上,你可以用time来获得执行时间:

 $ g++ myprog.cpp -o myprog $ time ./myprog 

只是一个侧面说明:如果你在Windows上运行,你真的需要精度,你可以使用QueryPerformanceCounter 。 它可以在(可能) 纳秒内给你时间。

你也可以使用clock_gettime 。 这种方法可以用来衡量:

  1. 全系统实时时钟
  2. 系统宽单调时钟
  3. 每进程CPU时间
  4. 每进程线程CPU时间

代码如下:

 #include < time.h > #include <iostream> int main(){ timespec ts_beg, ts_end; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_beg); clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_end); std::cout << (ts_end.tv_sec - ts_beg.tv_sec) + (ts_end.tv_nsec - ts_beg.tv_nsec) / 1e9 << " sec"; } 

`

对我来说,最简单的方法是:

 #include <boost/timer.hpp> boost::timer t; double duration; t.restart(); /* DO SOMETHING HERE... */ duration = t.elapsed(); t.restart(); /* DO OTHER STUFF HERE... */ duration = t.elapsed(); 

使用这段代码,你不必做经典的end - start

享受你最喜欢的方法。

以毫秒为单位获取系统时间,最后再减去系统时间。

为了在POSIX中获得1970年以来的毫秒数,你可以这样写:

 struct timeval tv; gettimeofday(&tv, NULL); return ((((unsigned long long)tv.tv_sec) * 1000) + (((unsigned long long)tv.tv_usec) / 1000)); 

要获得1601以来在Windows上的毫秒数,你可以这样写:

 SYSTEMTIME systime; FILETIME filetime; GetSystemTime(&systime); if (!SystemTimeToFileTime(&systime, &filetime)) return 0; unsigned long long ns_since_1601; ULARGE_INTEGER* ptr = (ULARGE_INTEGER*)&ns_since_1601; // copy the result into the ULARGE_INTEGER; this is actually // copying the result into the ns_since_1601 unsigned long long. ptr->u.LowPart = filetime.dwLowDateTime; ptr->u.HighPart = filetime.dwHighDateTime; // Compute the number of milliseconds since 1601; we have to // divide by 10,000, since the current value is the number of 100ns // intervals since 1601, not ms. return (ns_since_1601 / 10000); 

如果你关心规范化Windows答案,以便它也返回自1970年以来的毫秒数,那么你将不得不调整你的答案11644473600000毫秒。 但是,如果你关心的是经过的时间,这是没有必要的。

如果您正在使用:

 tstart = clock(); // ...do something... tend = clock(); 

那么你将需要以下几点来获得时间:

 time = (tend - tstart) / (double) CLOCKS_PER_SEC; 

这似乎英特尔Mac 10.7工作正常:

 #include <time.h> time_t start = time(NULL); //Do your work time_t end = time(NULL); std::cout<<"Execution Time: "<< (double)(end-start)<<" Seconds"<<std::endl;