打印函数调用时间

raii可以实现,通过new delete控制调用前后,很懒,只想加几个字符,不想换行

snippet mlt
#include <sys/time.h>

#define MLLOG printf
#ifndef MLLOG
#error "please define MLLOG"
#endif

struct MLT{
        MLT(struct timeval start_time, const char* label_tag)
        :t_start(start_time), label(label_tag)
        {
        }

        template <typename T>
        MLT& operator = (T)
        {
            return *this;
        }

        ~MLT()
        {
            gettimeofday(&t_end, 0);
            long time_diff = (t_end.tv_sec - t_start.tv_sec)*1000 + (t_end.tv_usec - t_start.tv_usec)/1000;
            MLLOG("%s cost time is %ld ms\n", label, time_diff);
        }
private:
    struct timeval t_start;
    struct timeval t_end;
    const char* label;
};

MLT mlt_helper(struct timeval start_time, const char* label_tag)
{

    return MLT(start_time, label_tag);
}

// caNNot use for void return functions
// and seems tedious
#define mlt(f)    struct timeval t_start; \
                  gettimeofday(&t_start, 0); \
                  mlt_helper(t_start, #f) = \


// cannot use for void return functions
#define mt        struct timeval t_start; \
                  gettimeofday(&t_start, 0); \
                  mlt_helper(t_start, "") = \
 

测试:

int f(void)
{
    float k = 0.0;
    for(auto i = 0; i < 10010; i++)
    for(auto m = 0; m < 10090; m++)
    {
        float j = ((float)i*4.19)/4.342;

        k += j;
    }
}
 

int main(int argc, char* argv[])
{
mlt(xxx)
f();

cout << "after " << endl;

    return 0;
}
有两个问题,void不能作为operator=的参数,所以benchmark的函数必须有返回值,mlt不能直接取f 的名称,导致要写个(),比较烦。懒。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值