一般设计C/C++程序需要每秒能处理多少的数据,因此可以做一个简单的计时器来计时,代码如下:
-
#ifndef _TIMER_H_
-
#define _TIMER_H_
-
#include <string>
-
#include <sys/time.h>
-
using namespace std;
-
class Timer{
-
private:
-
timeval tstart;
-
timeval tend;
-
unsigned count;
-
unsigned print_count;
-
public:
-
Timer():count(0),print_count(10000){
-
}
-
Timer(int pc):count(0),print_count(pc){
-
}
-
void add(){
-
count++;
-
if(count % print_count == 0){
-
end();
-
begin();
-
}
-
}
-
void begin(){
-
gettimeofday(&tstart, NULL);
-
}
-
void end(){
-
gettimeofday(&tend, NULL);
-
double linStart = ((double)tstart.tv_sec * 1000000 + (double)tstart.tv_usec);
-
double linEnd = ((double)tend.tv_sec * 1000000 + (double)tend.tv_usec);
-
double delta = (linEnd-linStart)/1000000;
-
printf("Timer:%d %d %f %f/n", print_count,count,delta,print_count/delta);
-
}
-
};
-
#endif /*_TIMER_H_*/
调用方式如下:
-
Timer timer(10000);
-
timer.begin();
-
for(;;){
-
timer.add();
-
}
-
timer.end();
C/C++写得一个计时器用于检查程序的处理数据性能
原文:http://blog.csdn.net/nyist327/article/details/44408285