Inlining function could be faster because it:
- Eliminate the cost of calling function. The cost will making some changes to some register, pushing thing to stack, and then a jump on the code.
- Enable more optimization on the caller since the compiler has more info on how a function works. Depending on the cases, this would be the larger benefit.
The drawback, however, is larger code size, and that could lead to more icache miss.
For a function to be inlined in C++, it needs to be defined on the header, or link time optimization is enabled.
Compiler on average can decide on which function, or where a function should be inlined better than human, especially with profile guided optimization. However, there are cases that you might want to use these attribute manually, e.g. if you know for sure a function is always in a critical path, and it will not lead to code bloat; or if the path that you want to optimize for is not hot (executed less frequently compared to other paths).
Beside these attributes, there are others inline-related one that could be helpful: flatten
Note that you might see different results on Clang, due to how these attributes are interpreted.
References: