天天看点

用c++实现记录程序运行时间的类

有时候性能测试需要统计qps,rt等参数, 我们可以在代码中加入时间戳来捕获一些请求所消耗的时间,如下就是我写的一个简单的Timer类:

#include<sys/time.h>

class Timer{

    struct timeval begin_t, end_t;

  public:

    void set();

    double get();//returns the milliseconds

};

void Timer::set() {

 gettimeofday(&begin_t, NULL );

}

double Timer::get() {

 gettimeofday( &end_t, NULL );

 return ( end_t.tv_sec - begin_t.tv_sec)*1000 + ( end_t.tv_usec - begin_t.tv_usec) /1000.0;

}

runing environment is Linux

继续阅读