一、clock
#include<ctime>
clock_t start,end;
start=clock();
end=clock();
cout<<start<<','<<end<<','<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
统计process时间
精确到0.01s

二、time
#include<ctime>
time_t start,end;
start=time();//time(&start)
end=time();
cout<<start<<','<<end<<','<<difftime(end,start)<<endl;
精确到1s

三、clock_gettime
#include<ctime>
struct timespec start,end;
//struct timespec {
// time_t tv_sec; /* seconds */
// long tv_nsec; /* nanoseconds */
// };
clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&start);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&end);
//CLOCK_REALTIME, a system-wide realtime clock.
//CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
//CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
cout<<start.tv_sec<<':'<<start.tv_nsec<<','<<end.tv_sec<<':'<<end.tv_nsec<<','<<end.tv_sec-start.tv_sec
<<':'<<end.tv_nsec-start.tv_nsec<<endl;
精确到1ns
编译需要带 -lrt参数
不适用windows?

四、chrono
#include<chrono>
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
end = std::chrono::system_clock::now();
cout<<std::chrono::system_clock::to_time_t(start)<<','<<std::chrono::system_clock::to_time_t(end)<<','
<<std::chrono::duration_cast<std::chrono::milliseconds> (end-start).count()<<endl;
//hours,minutes,seconds,milliseconds,microseconds,nanoseconds
//system_clock,steady_clock,high_resolution_clock
c++11标准,编译带 -std=c++0x 参数

 

posted on 2014-02-10 16:06  perel  阅读(394)  评论(0编辑  收藏  举报