Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function method definition in .cpp vs .h

Tags:

c++

In order to reduce the compilation time of a fairly large framework at work I was considering moving class method definitions in .h files to their associated .cpp file if they were either very large or required includes to compile that could be moved to the associated .cpp file. For clarity, below is a contrived example (although Foo::inc is a tiny method)

main.cpp:

#include "Foo.h"

int main(int argc, char** argv) {
    Foo foo(argc);
    foo.inc();
    return foo.m_argc;
}

Foo.h before (no need for Foo.cpp yet):

class Foo {
public:
    int m_argc;
    Foo (int argc) : m_argc(argc) {}
    void inc() { m_argc++; }
};

Foo.h after:

class Foo {
public:
    int m_argc;
    Foo (int argc) : m_argc(argc) {}
    void inc();
};

Foo.cpp:

#include "Foo.h"

void Foo::inc() { m_argc++; }

A long time ago a coworker mentioned that there may be cases where this can cause run time performance to slowdown. I was looking for this case on Google but could not seem to find it, the accepted answer to this question is the closest I could find but it does not give the case, just a mention that it can happen: Moving inline methods from a header file to a .cpp files

On a side note, I am not interested in the case where a method explicitly uses inline, the answer I linked above was just the closest I could find to what I was looking for

What case (if any) could cause a run time slowdown?

like image 286
asimes Avatar asked Jun 28 '15 19:06

asimes


People also ask

Can you define a function in a .h file?

The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.

What is .h and .CPP in C++?

The short answer is that a . h file contains shared declarations, a . cpp file contains definitions and local declarations. It's important that you understand the difference between declarations and definitions. A .

What is the difference between a .h and a CPP file what kind of code do we put in each?

h files are supposed to contain declarations whereas . cpp files are supposed to contain their definitions/implementation".

Does C++ have function or method?

Classes and their member functions (or methods) are integral features of the object-oriented C++ programming language. By tying these functions to an object's namespace, class methods make your C++ code modular and reusable.


2 Answers

Your original method void inc() { m_argc++; } was automatically inline so the compiler was allowed to replace the call with the inlined version.

As soon as you move the method out of the class definition to the module, this method is not inline anymore, the inline expansion doesn't happen, the standard method call is there, and the result can be slower.

like image 58
dlask Avatar answered Oct 09 '22 22:10

dlask


Reducing the header dependencies is always a good idea to reduce the compilation time. It's one of the top items in listings like What techniques can be used to speed up C++ compilation times?

I would recommend to - if not already done - take a look into the major players eating up your compilation time with Profiling the C++ compilation process

And there are helpers to sort through your include dependencies, see Automate #include refactoring in C++

About the question if moving code to the source files will slow down your runtime performance: It depends. Generally speaking you can say you give the compiler a chance to inline if you have the function in the header.

I like to quote from the C++ FAQ - Inlining:

Do inline functions improve performance?

Yes and no. Sometimes. Maybe.

There are no simple answers. inline functions might make the code faster, they might make it slower. They might make the executable larger, they might make it smaller. They might cause thrashing, they might prevent thrashing. And they might be, and often are, totally irrelevant to speed.

What the compiler - and maybe later the linker - does with it depends on what compiler toolchain you are using and what compiler/linker options you give.

See e.g. all the what-happens-when in inline - Using the GNU Compiler Collection:

... GCC does not inline any functions when not optimizing ...

Some references:

  • Inline speed and compiler optimization
  • Tool to parse C++ source and move in-header inline methods to the .cpp source file?
  • C++ Optimization Techniques - C++ Final Optimizations - Inline Functions
  • Optimizing C++/Writing efficient code/Performance improving features - Inlined functions
  • C++ Optimization Techniques
like image 32
Florian Avatar answered Oct 10 '22 00:10

Florian