c++ code time代码时汇总

.h 文件


/*
 * commonfunction.h
 *
 *  Created on: Sep 2, 2019
 *      Author: huanglei
 */

#ifndef COMMONFUNCTION_H
#define COMMONFUNCTION_H
using namespace std;
extern map<string, timespec> timerMap;
void start(const string& name);
double stop(const string& name);

.cpp 文件

common
#include "commonFunction.h"
map<string, timespec> timerMap;
void start(const string& name) {
//  timerMap.clear();
    timespec ts1;
    clock_gettime(CLOCK_REALTIME, &ts1);
    timerMap[name] = ts1;
}

double stop(const string& name) {
    int isDebugAnalogOn=0;
    timespec ts1 = timerMap[name];
    timespec ts2;
    clock_gettime(CLOCK_REALTIME, &ts2);
    double interval = (ts2.tv_sec - ts1.tv_sec)*1e3 + (ts2.tv_nsec - ts1.tv_nsec)/1e6;
//    GET_TESTFLOW_FLAG("debug_analog",&isDebugAnalogOn);
    if (isDebugAnalogOn) {
        cout << "DEBUG: timer " << name << " = " << interval << " ms  " ;
    }
    return interval;
}

用例:

#include "commonFunction.h"
void codeTime()
{
	start("timer")
	double codeTime=stop("timer")
	cout<<codeTime<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值