c++實現微妙級時間統計

/*
 * SYSstdTime.h
 *
 *  Created on: 2016年11月15日
 *      Author: root
 */

#ifndef SYSSTDTIME_H_
#define SYSSTDTIME_H_

#include "time.h"
#include <string>
#include <unistd.h>
#include <sys/time.h>


class time_statstic{
private:
    typedef struct timeval  timeer_t ;
public:
    explicit time_statstic( const std::string & str  , void* (*func)( void * ,char* ) ) ;
    ~time_statstic() ;
private:
    std::string m_str ;
    timeer_t start ;
    timeer_t end ;
    void*( *m_func)(void* ,char *) ;
};



#endif /* SYSSTDTIME_H_ */
/*
 * SYSstdTime.cpp
 *
 *  Created on: 2016年11月15日
 *      Author: root
 */

#include "SYSstdTime.h"

time_statstic::time_statstic( const std::string & str,void* (*func)( void * ,char*)  ):m_str(str)\
,m_func(func){
    gettimeofday(&start ,NULL ) ;
}

time_statstic::~time_statstic()
{
    gettimeofday(&end ,NULL ) ;
    long d=(end.tv_sec-start.tv_sec)*1000000+( end.tv_usec - start.tv_usec) ;
    (*m_func)( &d ,  (char*)m_str.c_str() ) ;
}


發佈了43 篇原創文章 · 獲贊 14 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章