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

来源:互联网 发布:51单片机代号 编辑:程序博客网 时间:2024/06/15 22:26

有时候性能测试需要统计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

原创粉丝点击