在c++中添加内联函数以提高性能
objective c - extern inline functions in C++ for performance
我有以下场景我有一个。cpp文件(我需要使用这些实际上在objective - c++ .mm):
#include <utility>
bool g_ready;
std::pair<int, bool> g_pair;
extern inline bool& isReady()
{
return g_ready;
}
extern inline std::pair<int, bool>& GetPair()
{
return g_pair;
}
extern inline void SetPair(int i, bool b)
{
g_pair.first = i;
g_pair.second = b;
}
然后我在另一个。cpp文件中使用这些:
#include <iostream>
extern inline bool& isReady();
extern inline std::pair<int, bool>& GetPair();
extern inline void SetPair(int i, bool b);
void update()
{
if (isReady())
{
SetPair(5, false);
std::cout << "<" << GetPair().first << "," << GetPair().second << ">" << std::endl;
SetPair(1, true);
std::cout << "<" << GetPair().first << "," << GetPair().second << ">" << std::endl;
}
}
我需要这些函数在每帧调用的Update循环中被调用。这是最优解吗?这些会在没有任何运行时开销的情况下内联吗?
编辑:产品编译器是Clang for iOS
这是最优解吗?
。内联函数必须在使用它们的每个翻译单元中定义,无论它们是否具有外部链接(默认情况下它们将具有外部链接,无需指定extern
)。因为这个原因,你的代码给了我一个链接错误。
这些将实际上内联没有任何运行时开销吗?
这取决于编译器。短inline
函数(必须在与调用相同的单元中定义)通常为;只有当编译器支持链接时优化时,其他翻译单元中的函数才能内联。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 如何在不使用函数或类的情况下重复代码段,以便在C++中实现高性能循环
- C++组合高性能函数
- C++ 中的高性能代码(继承、指向函数的指针、if)
- 如何在特征中实现高性能分段线性传递函数
- 在高性能计算中更好的做法是:将数据结构传递到一个函数或一组变量中