运算符和其他方法在C++中进行内联是否有任何区别?我已经搜索过它,但这不是一个常见的问题,正如我所看到的那样.有没有人有充分的理由使用它或避免?注意:显然,我的意思是内联运算符,当它们很小时.
我最近写了几堂课; 我想知道这是不好的做法,对性能有害,打破封装,还是在标题中实际定义一些较小的成员函数是否还有其他任何不妥之处(我确实试过谷歌!).这是一个我有一个标题的例子我写了很多这个:
class Scheduler {
public:
typedef std::list<BSubsystem*> SubsystemList;
// Make sure the pointer to entityManager is zero on init
// so that we can check if one has been attached in Tick()
Scheduler() : entityManager(0) { }
// Attaches a manager to the scheduler - used by Tick()
void AttachEntityManager( EntityManager &em )
{ entityManager = &em; }
// Detaches the entityManager from a scheduler.
void DetachEntityManager()
{ entityManager = 0; }
// Adds a subsystem to …Run Code Online (Sandbox Code Playgroud) 我希望利用kotlin的表达式和通用方法来简化Android的共享首选项api.
而不是一直调用getString()和getInt()等,我想要做的是创建一个扩展函数,它将根据函数的返回类型进行切换并调用适当的方法.如下所示:
fun <T> SharedPreferences.get(key: String): T? {
when (T) { //how do I switch on return type and call appropriate function?
is String -> getString(key, null)
is Int -> getInt(key, -1)
is Boolean -> getBoolean(key, false)
is Float -> getFloat(key, -1f)
is Long -> getLong(key, -1)
}
return null
}
Run Code Online (Sandbox Code Playgroud)
当然,它不会起作用.但是当表达式为函数的返回类型时,是否有任何解决方案?欢迎所有建议.
以下代码有什么区别:
class Foo
{
inline int SomeFunc() { return 42; }
int AnotherFunc() { return 42; }
};
Run Code Online (Sandbox Code Playgroud)
两个函数都会被内联吗?内联实际上有什么区别吗?关于何时应该或不应该内联代码,是否有任何规则?我经常使用AnotherFunc语法(例如访问器),但我很少inline直接指定.
请考虑以下代码:
#include <algorithm>
#include <chrono>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v(12);
std::iota(v.begin(), v.end(), 0);
//std::next_permutation(v.begin(), v.end());
using clock = std::chrono::high_resolution_clock;
clock c;
auto start = c.now();
unsigned long counter = 0;
do {
++counter;
} while (std::next_permutation(v.begin(), v.end()));
auto end = c.now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << counter << " permutations took " << duration.count() / 1000.0f << " s";
}
Run Code Online (Sandbox Code Playgroud)
-O2在我的AMD 4.1 GHz CPU上使用GCC(MinGW)5.3 进行编译2.3 s.但是,如果我在未注释的行中发表评论,它会减慢到3.4 s …
带有警卫的头文件中定义的非内联函数
#if !defined(HEADER_RANDOM_H)
#define HEADER_RANDOM_H
void foo()
{
//something
}
#endif
Run Code Online (Sandbox Code Playgroud)
链接器错误中的结果:已在someother.obj文件中定义使函数内联工作正常但我无法理解为什么函数在第一种情况下已经错误输出.
为了减少工作中相当大的框架的编译时间,我考虑将.h文件中的类方法定义移动到它们关联的.cpp文件中,如果它们非常大或者需要包括编译可以移动到关联的文件. cpp文件.为清楚起见,下面是一个人为的例子(虽然Foo::inc是一个很小的方法)
main.cpp:
#include "Foo.h"
int main(int argc, char** argv) {
Foo foo(argc);
foo.inc();
return foo.m_argc;
}
Run Code Online (Sandbox Code Playgroud)
之前是Foo.h(还不需要Foo.cpp):
class Foo {
public:
int m_argc;
Foo (int argc) : m_argc(argc) {}
void inc() { m_argc++; }
};
Run Code Online (Sandbox Code Playgroud)
Foo.h之后:
class Foo {
public:
int m_argc;
Foo (int argc) : m_argc(argc) {}
void inc();
};
Run Code Online (Sandbox Code Playgroud)
Foo.cpp:
#include "Foo.h"
void Foo::inc() { m_argc++; }
Run Code Online (Sandbox Code Playgroud)
很久以前,一位同事提到可能存在导致运行时性能下降的情况.我在谷歌上寻找这个案子,但似乎无法找到它,这个问题的接受答案是我能找到的最接近的答案,但它没有给出案例,只是提到它可能发生:从标题中移动内联方法文件到.cpp文件
在旁注中,我对方法明确使用的情况不感兴趣,inline我上面链接的答案就是我能找到的最接近我正在寻找的方法
什么情况(如果有的话)可能导致运行时间减慢?
如果我内联一个函数.函数调用体将被复制,而不是向它发出一个call().为什么会导致糟糕的表现呢?
编辑:然后由于大功能而缓存未命中呢?为什么经验法则"仅存在最多3行的内联函数"?
可能重复:
C++中内联函数的好处?
我对内联函数感到困惑.
人们说内联函数通过用原始代码替换函数来节省CPU时间,但是与普通函数相比,它增加了代码的大小.
所以真正的问题是如果我继续在循环中调用内联函数10次,代码大小是否会增加.
假设内联函数大小为2个字节,它会增加20个字节吗?
有谁可以向我解释一下?
可能重复:
C++中内联函数的好处?
有什么区别
#include <iostream>
using namespace std;
int exforsys(int);
void main( )
{
int x;
cout << "n Enter the Input Value: ";
cin>>x;
cout << "n The Output is: " << exforsys(x);
}
int exforsys(int x1)
{
return 5*x1;
}
Run Code Online (Sandbox Code Playgroud)
和
#include <iostream>
using namespace std;
int exforsys(int);
void main( )
{
int x;
cout << "n Enter the Input Value: ";
cin>>x;
cout << "n The Output is: " << exforsys(x);
}
inline int exforsys(int x1)
{ …Run Code Online (Sandbox Code Playgroud) c++ ×9
function ×4
header ×2
android ×1
c ×1
definition ×1
generics ×1
inline ×1
inlining ×1
kotlin ×1
member ×1
operators ×1
performance ×1
permutation ×1
types ×1