fllprbt
fllprbt

Reputation: 113

Inline vs Normal Function in C++

I have just finished inline functions chapter in Bruce Eckel's : thinking in C++. Well there's an exercise which asks you to create two exact functions one being inline and the other not. Then use clock() and count the time passed for each one. I have worked on similar problems and I don't think there's anything complex with it. Therefore I came up with :

#include <iostream>
#include <ctime>
using namespace std;

inline int infun(int x) {
    x = 3;
    x = 5;
    cout << "";
    return x;

}

int fun(int x) {
    x = 3;
    x = 5;
    cout << "";
    return x;

}

int main() {

    clock_t startIn = clock();
    for (int i = 0; i < 10000000; i++) {
        infun(i);

    }
    clock_t finishIn = clock();

    clock_t start = clock();
    for (int i = 0; i < 10000000; i++) {
        fun(i);

    }
    clock_t finish = clock();

    clock_t startIn2 = clock();
    for (int i = 0; i < 10000000; i++) {
        infun(i);

    }
    clock_t finishIn2 = clock();

    cout << "Inline: " << (finishIn - startIn) << endl << "Regular Function: "
            << (finish - start) << endl<< "Second Inline: " << finishIn2 - startIn2 << endl;
    return 0;
}

Output

Inline: 195842
Regular Function: 166564
Second Inline: 162917

So I have 3 functions. 2 exactly similar inlines and one non-inline (for testing purposes I came up with this case).

a) Why the first inline takes all that time (the same happens for any function executed first) b) Why if the repetitions are decreased (lets say 1000) the normal function is faster than the others.

My test cases are satisfied even with simpler functions like :

inline int infun(int x) {
    return x; 
}

I've also checked the assembly output to ensure that the inlines are truly inlines or that g++ does not promote the non-inline to inline. Thank you for your time, any feedback is appreciated.

Upvotes: 3

Views: 1966

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69882

paraphrasing from cppreference.com:

An inline function is a function with the following properties:

1) There may be more than one definition of an inline function in the program as long as each definition appears in a different translation unit. For example, an inline function may be defined in a header file that is #include'd in multiple source files.

2) The definition of an inline function must be present in the translation unit where it is accessed (not necessarily before the point of access).

3) An inline function with external linkage (e.g. not declared static) has the following additional properties:

1) It must be declared inline in every translation unit. 2) It has the same address in every translation unit.

Notice that not once has optimisation, performance or actual inlining of machine code been mentioned or even hinted at.

inline simply says to the compiler: "this function may be defined more than once, but I promise that each definition is identical"

Upvotes: 5

Related Questions