C++ Inline Functions
Optimizing function calls for better performance
โก What are Inline Functions?
Inline functions are a performance optimization where the compiler replaces function calls with the actual function code. This eliminates the overhead of function calls for small, frequently used functions.
// Inline function definition
inline int square(int x) {
return x * x;
}
// Compiler replaces square(5) with (5 * 5)
Performance Benefit:
No function call overhead - code runs faster!
Inline Function Benefits
Speed
Eliminates function call overhead
inline int add(int a, int b) { return a + b; }
Code Expansion
Function code is inserted at call site
// add(3, 4) becomes (3 + 4)
Small Functions
Best for short, simple functions
inline bool isEven(int n) { return n % 2 == 0; }
Compiler Hint
Suggestion to compiler, not guarantee
// Compiler decides final inlining
๐น Basic Inline Function
Inline functions suggest to the compiler to insert the functionโs code directly at the call site, avoiding
overhead. Defined with the inline keyword, they are ideal for small, frequently called
functions like max() or abs(). Inlining can improve performance by eliminating call
overhead but may increase binary size. Use inline for simple operations where speed is critical and the function
body is small, typically in headers, utility classes, or performance-sensitive sections of C++ code.
#include <iostream>
using namespace std;
// Inline function for maximum of two numbers
inline int max(int a, int b) {
return (a > b) ? a : b;
}
// Inline function for absolute value
inline int abs(int x) {
return (x < 0) ? -x : x;
}
int main() {
int x = 10, y = 20;
cout << "Max of " << x << " and " << y << " is: " << max(x, y) << endl;
cout << "Absolute value of -15 is: " << abs(-15) << endl;
return 0;
}
Output:
Max of 10 and 20 is: 20
Absolute value of -15 is: 15
๐น Inline vs Regular Functions
Inline functions reduce call overhead by expanding code in place, while regular functions maintain separate callable units. Inlining speeds up execution for trivial functions but can bloat code if overused. Regular functions save space and support recursion or indirect calls. The choice depends on trade-offs: inline for performance-critical, small functions; regular for complex or large routines. Understanding this balance is key for optimizing C++ programs, especially in embedded systems or high-performance computing.
#include <iostream>
#include <chrono>
using namespace std;
// Regular function
int regularSquare(int x) {
return x * x;
}
// Inline function
inline int inlineSquare(int x) {
return x * x;
}
int main() {
const int iterations = 1000000;
// Test regular function
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
regularSquare(i);
}
auto end = chrono::high_resolution_clock::now();
auto regularTime = chrono::duration_cast<chrono::microseconds>(end - start);
// Test inline function
start = chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
inlineSquare(i);
}
end = chrono::high_resolution_clock::now();
auto inlineTime = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Regular function time: " << regularTime.count() << " microseconds" << endl;
cout << "Inline function time: " << inlineTime.count() << " microseconds" << endl;
return 0;
}
Typical Output:
Regular function time: 2500 microseconds
Inline function time: 1200 microseconds
๐น Class Member Inline Functions
Functions defined inside a class declaration are implicitly inline, promoting encapsulation and potential
performance gains. For example, a Rectangle class with
int area() { return width*height; } defined in-class is inline. This keeps related code together, can
improve speed for simple accessors or mutators, and is common in C++ classes. However, complex logic should be
defined outside to avoid code bloat. Implicit inlining aids writing clean, efficient class interfaces in
object-oriented design.
#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Constructor (automatically inline)
Rectangle(int w, int h) : width(w), height(h) {}
// Inline member functions
int getArea() { return width * height; }
int getPerimeter() { return 2 * (width + height); }
// Explicitly inline function outside class
inline void display();
};
// Definition outside class with inline keyword
inline void Rectangle::display() {
cout << "Rectangle: " << width << "x" << height << endl;
}
int main() {
Rectangle rect(5, 3);
rect.display();
cout << "Area: " << rect.getArea() << endl;
cout << "Perimeter: " << rect.getPerimeter() << endl;
return 0;
}
Output:
Rectangle: 5x3
Area: 15
Perimeter: 16
๐น When NOT to Use Inline
Inline functions aren't always beneficial:
Avoid inline for:
- Large functions: Increases code size significantly
- Recursive functions: Cannot be effectively inlined
- Functions with loops: Usually too complex for inlining
- Virtual functions: Cannot be inlined in most cases
// DON'T inline large functions
inline void processLargeData() {
// 50+ lines of code
// This will increase executable size
}
// DON'T inline recursive functions
inline int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n-1); // Recursion prevents inlining
}