程式運作耗時計算
常用的程式運作耗時計算有四中方法:
1,clock_t 頭檔案為time.h
clock_t start = clock();
function();
clock_t end = clock();
std::cout<<"time is:"<<(double)(end -time)/CLOCKS_PER_SEC<<std::endl;
2,利用gettimeofday()
struct timeval tpstart,tpend;
float timeuse;
gettimeofday(&tpstart,NULL);
function();
gettimeofday(&tpend,NULL);
timeuse=(*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/;
qDebug()<<timeuse<<"s";
3,利用opencv中計時函數
int64 start = getTickCount();
function();
int64 end = getTickCount();
std::cout<<"opencv: "<<(end-start)/getTickFrequency()<<std::endl;
4,利用Qt中的QTime
QTime time;
time.start();
function();
std::cout<<"time is:"<<time.elapsed()<<std::endl;
通過比較這幾種計時方法,分為幾種情況:
1,在單線程工作中,計時方法準确度幾乎是一緻的。
2,在多線程時候,opencv的方法和qtime的方法保持一緻,clock_t計時方法和其他有一定出入。
3,在GPU參與運算的時候,opencv方法和qtime方法保持一緻。
以上的計時結果都是大同小異,差别不是很大。底層的計時方法是計算cpu的時鐘。