.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;
}