#define MAX_CALL_DEPTH 10
uint32 prevTime[MAX_CALL_DEPTH];
uint32 postTime[MAX_CALL_DEPTH];
uint32 callDepth;
void calcTimeInit(void)
{
memset(prevTime, 0x00, sizeof(prevTime));
memset(postTime, 0x00, sizeof(postTime));
callDepth = 0;
}
void calcTimePrev(void)
{
if(0!=prevTime[callDepth])
callDepth++;
callDepth %=MAX_CALL_DEPTH;
prevTime[callDepth] = getTimeFunc();
}
void calcTimePost(void)
{
callDepth %=MAX_CALL_DEPTH;
postTime[callDepth] = prevTime[callDepth] = getTimeFunc();
}
uint32 calcTimeGetDiff(void)
{
uint32 diff = postTime[callDepth] – prevTime[callDepth];
postTime[callDepth] = prevTime[callDepth] = 0;
if(callDepth>0)
callDepth–;
return diff;
}
用法:
calcTimeInit();
…..
calcTimePrev();
……
you code 1
……
calcTimePost();
printf(" Total time 1: %dn", calcTimeGetDiff());
…..
calcTimePrev();
……
you code 2
……
calcTimePost();
printf(" Total time 2: %dn", calcTimeGetDiff());
转载请注明:在路上 » 【API】计算代码运行时间的函数框架及用法