c / intermediate
Snippet
Inlining Functions for Performance
The 'inline' keyword is a hint to the compiler to replace function calls with the actual function body. This eliminates function call overhead (stack manipulation, jumping) for small, frequently used functions, potentially improving execution speed.
snippet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>// Suggests to the compiler to integrate the code directly at call sitesstatic inline int max(int a, int b) {return (a > b) ? a : b;}int main() {int x = 42, y = 15;// The compiler might replace the call with: int result = (42 > 15) ? 42 : 15;int result = max(x, y);printf("Max value: %d\n", result);return 0;}
Breakdown
1
static inline int max(...)
Declares a function as inline; 'static' is often used to limit scope to the current translation unit.
2
return (a > b) ? a : b;
Small logic suitable for inlining as it is computationally cheap.