c++实现微妙级时间统计

本文介绍了一个用于记录函数执行时间的C++类`time_statstic`。该类通过`gettimeofday`函数获取时间戳,计算函数执行前后的时间差,并调用传入的回调函数输出结果。适用于性能分析和优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * 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() ) ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值