Click here to Skip to main content
15,913,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi :)
Our C++ exam is coming up and I don´t know the answer for this possible question:

Are inline functions executed faster when they are defined in the header file or the implementation file?

Could someone tell me the correct answer and the reason? I really struggle understanding this. Thank you so much in advance! :)

What I have tried:

I went through our lectures and tried to research it but the things that I did find are just too complicated for me :/
Posted
Updated 1-Jun-16 12:58pm
Comments
Sergey Alexandrovich Kryukov 1-Jun-16 12:54pm    
There are no alternatives here. Inline is inline. Files are irrelevant to compilation. For C++, files are just fed to compilation in certain order; there are no difference between .h and .cpp files.
—SA
CPallini 1-Jun-16 16:27pm    
Short answer: no.
Long answer: nooooooooooo.

Files are not essential to C++ compilation. Essentially, file-level modularity is just a matter of project development, maintainability, things like that — very important but not related to the result of compilation. In particular, there is no difference between .h and .cpp files (I am not talking about subtle detail, such as precompiled headers; I'm talking only about the results of compilation.) The files are just fed to the compiler in certain order, that's it.

The concept of file-level modularity is based on the C++ feature to allow multiple declarations and only one definition, and then on the linked which operates on the image names (string), that's all. (And, frankly, this conception is awfully archaic; I don't like it at all; it's no match to more advanced concepts used in some other languages. The techniques like precompiled headers is nothing but crutches.)

Indeed, inline function can give you some minor gain in performance. Note that a compiler may or may not take the benefit of inline functions. You can consider inline syntax as a recommendation to the compiler. Everything is explained, for example, here: What is C++ inline functions — C++ Articles[^].

To find out how much is the influence of this technique in each particular environment, you can perform timing of operation or profiling.

—SA
 
Share this answer
 
v3
Comments
CPallini 1-Jun-16 16:27pm    
5.
Sergey Alexandrovich Kryukov 1-Jun-16 18:49pm    
Thank you, Carlo.
—SA
Have also a look at this page: Standard C++: Inline Functions[^].
 
Share this answer
 
Quote:
Are inline functions executed faster when they are defined in the header file or the implementation file?
Not faster, not slower, exact same speed, exact same result.
Efficiency do not depend on the file extension used to define the inline function.
Even id you invent you own file extensions, it wouldn't change anything.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900