時間是用來評價一個算法或代碼的重要指標。
clock_t 為時鍾周期數,在並行程序中這種方式不能測量時間。
#include <time.h> clock_t start,finish; start = clock(); finish = clock(); std::cout<<"完成需要 "<<finish - start<<"個時鍾周期"<<std::endl; std::cout<<"消耗時間為:"<<(finish - start)/CLOCKS_PER_SEC*1000<<"ms\n"<<std::endl; // CLOCKS_PER_SEC 每秒德時鍾周期數
double omp_get_wtime() 返回絕對時間,單位為s
#include<omp.h> double start = omp_get_wtime( ); double end = omp_get_wtime( );
double omp_get_wtick() 返回單個時鍾周期的時間,s
#include "omp.h" #include <stdio.h> //獲得一個時鍾周期是多少秒 double wtick = omp_get_wtick( ); printf("wtick = %.16g\n1/wtick = %.16g\n", wtick, 1.0 / wtick); // 每s的時鍾周期數