class Timetest{ public: Timetest() { start = new timeval; } int start_test_time() { gettimeofday(start,0); } int stop_test_time(const char* info) { struct timeval result; struct timeval stop; gettimeofday(&stop,0); int diff_usec = -1; /* 计算start位置到当前位置的时间差 */ if ( start->tv_sec > stop.tv_sec ) { printf("qh ---the time is error\n"); return -1; } if ( (start->tv_sec == stop.tv_sec) && (start->tv_usec > stop.tv_usec) ) { printf("qh ---the time is error\n"); return -1; } result.tv_sec = ( stop.tv_sec - start->tv_sec ); result.tv_usec = ( stop.tv_usec - start->tv_usec ); if (result.tv_usec<0) { result.tv_sec -= 1; result.tv_usec+=1000000; } diff_usec = result.tv_usec/1000 + result.tv_sec*1000; printf("qh diff [%d]ms:%s\n", diff_usec, info); return diff_usec; } ~Timetest() { if(NULL != start) { delete start; start = NULL; } } private: struct timeval *start; }; |
|