Sign in - Sign up






Article by Majid Khan
Inline functions: large vs small functionsJul 17,2012 at 21:01
Category: Programming - General

Background

I have struggled heaps to teach myself C++. One of the keys of my clear concepts are making a lot of mistakes. And I mean A LOT!

In my last article [ilike in C++ same as SQL] (click here to read) I made changes. Because writing an article means a lot to me. It simply means that I am helping someone clear their concepts. Initially, I had derived std::string for sake of function extensions and then I realized I should not; since it is not recommended to derive from standard string. So, I was porting functions from derived string to utility string functions. While, I was doing that I came across this issue of choosing between having an inline function vs having a class with static functions.

Inline Functions

In C++, inline functions are a lot like placeholder. Means, when you define them, compiler will replace the function call with actual code. You might ask yourself, then what are inline functions good for? Well, short answer is, they save time and hassle of writing code again. Also, make code more clearer and faster. (Faster [for small in-line functions] because it does not have to go to v-table and find definition)

Size of Inline Function

Size of inline functions have big impact on efficiency. Bigger the inline function is, lower the efficiency would be. Take a look at following example
Example 1: Inline Function
1 #include <iostream>
2 inline void print(void){
3     std::cout<<"This is a print function";
4     std::cout<<"This is inline function";
5 }
6 void main(void){
7     print();
8     std::cin.get();
9 }
This code is simple enough to explain what happens under the hood. So when compiler compiles line 7 it actually replace that function call with actual code. So, after compilation, main funciton looks like:
1 void main(void){
2     std::cout<<"This is a print function";
3     std::cout<<"This is inline function";
4     std::cin.get();
5 }
Looking At this result, anyone would be able to tell that code is "expanding". Consider this code
Example 2: Multiple Function Calls
1 void main(void){
2     print();
3     print();
4     print();
5     std::cin.get();
6 }
What are cons of above code? It will eventually get slower depending on number of lines the inline function has. Compiler will replace each function call with the actual code, eventually replacing a lot of lines with a lot of more lines. The above calls line 2-4 will not cause too much of trouble. main function will look like
1 void main(void){
2     std::cout<<"This is a print function";
3     std::cout<<"This is inline function";
4     std::cout<<"This is a print function";
5     std::cout<<"This is inline function";
6     std::cout<<"This is a print function";
7     std::cout<<"This is inline function";
8     std::cin.get();
9 }
This is one reason why Google does not use inline functions if functions are more than 10 lines longer.click here for reference.



Read more article   |  Write an article   |  Report this article   |  Share this article




HomeLearn C++Source CodesArticles
    Recent Videos     Video Training     Browse     Read an article      Terms & Conditions
    Recent Articles     Supporting Docs     Upload My Code     Write an article      About Us
    Events     Source Codes     Search an article                Hide This Bar