统计代码运行时间计时器

linux

#include <sys/time.h>  
 
#include <iostream>  
 
#include <time.h>  
 
double get_wall_time()  
 
{  
 
    struct timeval time ;  
 
    if (gettimeofday(&time,NULL)){  
 
        return 0;  
 
    }  
 
    return (double)time.tv_sec + (double)time.tv_usec * .000001; 
 
}  
 
  
 
int main()  
 
{  
 
    unsigned int t = 0;  
 
    double start_time = get_wall_time(); 
 
    while(t++ < 10e+6);  
 
    double end_time = get_wall_time() ;
 
    std::cout<<"循环耗时为:"<<end_time-start_time<<"ms";  
 
    return 0;  
 
} 

windows

#include <windows.h>  
 
#include <iostream>  
 
  
 
int main()  
 
{  
 
    DWORD start, stop;  
 
    unsigned int t = 0;  
 
    start = GetTickCount();  
 
    while (t++ < 10e+6);  
 
    stop = GetTickCount();  
 
    printf("time: %lld ms\n", stop - start);  
 
    return 0;  
 
}  

猜你喜欢

转载自www.cnblogs.com/Malphite/p/10952791.html