3

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.

7
  • 3
    What compiler options did you use? Measuring an un-optimized build is not going to give you the correct information. Commented Dec 22, 2016 at 23:57
  • inline is simply a recommendation, the compiler makes its own determination on whether to actually inline the code. From there cache effects might give different timing on the exact same code. Also there's a lot of granularity in the standard clock, you might see a huge difference that isn't real. Commented Dec 23, 2016 at 0:01
  • Thanks for your input, I have used the auto generated build options of Eclipse and I've just tested g++ with no options. Same results. The author has not referred yet to any special building options. To add, the notion of the book's exercises so far is more or less some playful experience with the theory (not really special problems). The result had to be solid as the theory and in the case there was a conflict the author would probably ask why. Commented Dec 23, 2016 at 0:09
  • Modern compilers can figure out automatically which functions will benefit from inlining, and they're likely to ignore the keyword. Commented Dec 23, 2016 at 0:09
  • @Barmar: Modern compilers better not ignore that keyword, or else your program may fail to compile from duplicate definitions. However they might not consider it in the optimizer. Commented Dec 23, 2016 at 0:15

1 Answer 1

5

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"

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your comment. The book though raises the efficiency issue so many times even making suggestions when to use inline for efficiency. That is the very essence of this exercise as well, finding out which one is faster.
@fllprbt the meaning of inline has changed in C++, so simply put: the compiler will not "inline" a function just because you marked it to do so. The only purpose of inline in C++ is to tell the compiler a function definition is identical across translation units.
@fllprbt I'll expand upon my comment. I had a quick look at the book you mention. It was written almost 20 years ago. Back then, there were a great many c programmers who thought they knew c++ (but didn't). It's also fair to say that the c++ standard was not as mature as it is today. The confusion is understandable. However, if Mr Eckel is still teaching, he ought to up his game and/or revise his book. It is misleading people.
@RichardHodges The book was suggested by my dissertation's supervisor who relates for sure to an older period. Just recently I found out that it was not that good but this last issue puts me really into thinking of changing. Thank you for the feedback.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.