映射查找函数调用 Vs 将查找缓存为静态变量
map lookups function calls Vs caching the lookup as a static variable
函数版本一,map stl
查找一次并保存在局部静态变量中
inline const string & dataInputPath()
{
static string inputpath = Mngr.getStr("input");
return inputpath;
}
函数版本二,每次调用函数时都会循环
inline const string & dataInputPath()
{
return Mngr.getStr("input");
}
我有两个函数调用,我想知道
- 什么被认为更合适?
- 优点和缺点?
- 您更喜欢使用哪一个,为什么?
- 如果有办法将结果缓存为右值
getStr
返回类型const string&
首先,一旦调用了 dataInputPath,即使映射中的值发生了更改,该值也不会更新。
我只会直接从地图中检索值。如有必要,将其留给调用代码以合理的方式缓存值。
既然键"输入"是硬编码的,为什么不只使用全局input_string变量呢?
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 在C++中查找文件
- #为""定义宏;静态";针对不同的上下文
- 使用gcc从静态链接的文件中查找可选符号
- 在链接可执行文件之前查找静态库未解析的依赖项
- Cmake 查找模块用于区分共享库或静态库
- 动态链接到 c++ 静态成员字段时符号查找失败
- C++-如何制作查找矩阵的静态字典
- 如何查找全局静态初始化
- 映射查找函数调用 Vs 将查找缓存为静态变量
- 在 Linux 库中查找静态函数地址
- xlC模板函数问题的静态函数查找
- VS2010查找调试符号时,静态库链接
- 查找指向对象的静态指针
- 使用模板生成静态查找表
- 什么名称查找规则适用于静态 const 数据成员定义中的名称
- 在c++中创建并初始化一个静态const char数组作为Ascii查找表
- 如何查找函数在编译时是否为静态函数
- C++:在初始值设定项中类静态变量的定义中查找名称