|
|
|
|
|
by hDeraj
3580 days ago
|
|
When calling a simple function like this within a large loop, would it make a noticeable difference in speed to inline the computation vs. having a function call? If so, what's the best practice for inlining a computation like this? I imagine a macro would be the simplest solution but I'm interested to hear any other techniques that are used |
|
Maybe. Inlining small functions can reduce cache load, and it means no call/ret instructions and no overhead of argument passing. Moreover inlining allows futher optimizations which can't be done without breaking function boundaries. It may be noticeable. And may be not. Depends on loop.
> what's the best practice for inlining a computation like this?
There are a lot of examples can be seen in linux kernel. Just random example from include/linux/list.h:
Keyword 'static' allows compiler to make no callable (not inlined) copy of function at all, and also it allows to define such a functions in header files. Compiler can't inline function call if it has no function definition at compile time. Declaration is not enough for inlining. Therefore such a functions likely to be defined in headers, and 'static' becomes necessary. When defined in *.c file 'static' can be omitted, but probably better not to.With C++ such a functions will be a methods in most cases, and (if so) "static" would be unneeded and wrong.
And you'll need to turn on optimizations when compiling. Compiler is not inlining when not optimizing.