Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Performance/memory optimization guidelines

Does anyone have a resource for C++ memory optimization guidelines? Best practices, tuning, etc?

As an example:

Class xxx {

    public: 
        xxx();
        virtual ~xxx();

    protected:

    private:

};

Would there be ANY benefit on the compiler or memory allocation to get rid of protected and private since there there are no items that are protected and private in this class?

UPDATE: What is a programmer does this:

Class xxx {

    public: 
        xxx();
        virtual ~xxx();

    public:
        more stuff();
        more();

   ifndef __BUILD_WIN__
      public:
        even more();
        envenmore2();
   endif
    protected:

    private:

};
like image 391
ML. Avatar asked Mar 31 '10 18:03

ML.


People also ask

What are the memory optimization techniques?

Memory optimization is a range of techniques related to improving computer memory, such as identifying memory leaks and corruption, to optimize memory usage and increase performance and application usability.

What is optimize C?

Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed. In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes.


2 Answers

Does anyone have a resource for C++ memory optimization guidelines? Best practices, tuning, etc?

That depends a lot on both your compiler and target environment (RISC, Unix/Linux, Windows). Most compilers will have such information.

There are utilities out there that enable you to trace memory leaks so that you can fix them during test. If you are going to dynamically allocate a lot of things (which usually is the case with C/C++), try to make sure you deallocate everything before destroying an object. To do this:

  • If you value memory over processor, use smart pointers.
  • If your class has any member variables that are pointers, make sure your destructor frees each one of the. Group your member variables together on your source code so that it's easy to compare those variables with the destructor.
  • Avoid dynamic memory allocation whenever possible to avoid leaks. Prefer std::string over dynamic allocated char*, etc.

Would there be ANY benefit on the compiler or memory allocation to get rid of protected and private since there there are no items that are protected and private in this class?

No, if I'm not mistaken, protected/private are only checked during compilation, so they don't affect performance even if there were items under the keywords.

Furthermore, it's important to understand that the compiler is very inteligent (usually more than the programmer) so it will optimized away anything it can; For example, let's you declare a variable, int a, inside your constructor. And let's say you don't use it at all, you just forgot it there. Most of the compilers won't even save stack space to those variables. Others will need the user to activate Optimization so that this happens, but as a rule-of-thumb, your production version of any program should be compiled with optimization enabled, even if not on full.

About the update, that thing you looked at are pre-processors directives and are being used to do what is called selective compilation. Take a look here.

like image 199
Bruno Brant Avatar answered Sep 29 '22 21:09

Bruno Brant


Well the compiler wouldn't have to parse them, so there's that benefit to the compiler. For what that's worth (not very much).

There should be no memory use difference.

Other than that, the only benefit I can think of is there's less cruft for someone reading the code to have to deal with (not that it's particularly burdensome in your example).

like image 22
Michael Burr Avatar answered Sep 29 '22 20:09

Michael Burr