//GetFuncCallTimes.h
#ifndef _GETFUNCCALLTIMES_H
#define _GETFUNCCALLTIMES_H
#include <map>
#include <string>
class CGetFuncCallTimes
{
public:
std::map<std::string, long> call_count;
void Increase(std::string s)
{
++call_count[s];
}
long Count(std::string s)
{
return call_count[s];
}
};
extern CGetFuncCallTimes fct;
#endif
//GetFuncCallTimes.cpp
#include "GetFuncCallTimes.h"
CGetFuncCallTimes fct;
使用方法如下:
在源文件中包含该头文件: #include "GetFuncCallTimes.h"
在需要统计的函数里面加上一行: fct.Increase("function1");
输出时: long lTimes = fct.Count("function1");
本文介绍了一个简单的函数调用次数统计工具的实现方法。通过定义一个类`CGetFuncCallTimes`,利用`std::map`来记录不同函数被调用的次数,并提供了增加调用次数及获取当前调用次数的方法。只需在需要统计的函数中加入一行代码即可实现统计。
2136

被折叠的 条评论
为什么被折叠?



