开发者

C++ Performance/memory optimization guidelines

开发者 https://www.devze.com 2022-12-25 20:32 出处:网络
Does anyone have a resource for C++ memory optimization guidelines? Best practices, tuning, etc? As an example:

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();
开发者_如何学Go
   ifndef __BUILD_WIN__
      public:
        even more();
        envenmore2();
   endif
    protected:

    private:

};


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.


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).


There's "Efficient C++: Performance Programming Techniques" by Dov Bulka and David Mayhew. I don't think it's ground-breaking, but it certainly is an interesting read that teaches a few basics.


public, protected and private keywords, do not make it into object files at all so there is no benefit at all.

In general

  • Try to avoid allocating and freeing memory, reuse whenever possible. Walking the heap is especially slow.
  • Free memory when you are finished with it. Don't leave in hanging around unless there are strong performance reasons for doing so.

Truth is, unlike Java, when programming in C++, you always need to think about memory management to avoid leaks. Optimization therefore comes more naturally than say Java


As far as general guides, there's What Every Programmer Should Know About Memory, but I'm not sure it's what you're looking for.

One general guideline for optimizing memory inside a struct or class is to order your data members from largest to smallest. This prevents excess padding from wasting space in your struct or class.

As to your question I think it's been suitably answered, but you can compare yourself using sizeof().


It really depends on your application. If you are doing simulations or any kind of scientific computing where you are doing floating point calculations on large arrays of memory then there are a lot of things you can do.

  • Optimize for cache reuse (cache blocking, padding, etc)
  • Optimize to minimize TLB hits (page faults)
  • Arrange your data as structures of arrays, not as arrays of structures
  • Avoid unnecessary memory clears
  • Avoid reallocating memory, reuse instead

If you need to have many small objects, look at things like the flyweight design pattern or object pools.

As for worrying about private and protected declarations, don't. They are only relevant for compilation to enforce encapsulation and data hiding, and do not affect the generated binary.


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. AFAIK, non-virtual methods doesn't increase size of class instance.

Does anyone have a resource for C++ memory optimization guidelines?

1) If you are concerned about performance, use profiler (AQtime, for example). Do not guess, use tools.
2) In general, it is not a good idea to frequently (several _millions_ times per second) allocate and deallocate memory (using new/delete), especially large blocks. ACcording to my experience such usage results performance loss. If you frequently need to allocate large block of memory withing the same routine (using new or std::vector), consider reusing the same block in the next call (this will be tricky in multithreaded app). I.e. instead of

void doStuff(int memSize){
    std::vector<char> buf(memSize);
    //..some code here
}

use something like this:

void doStuff(int memSize){
    static std::vector<char> buf;
    if (buf.size() < memSize)
        buf.resize(memSize);
    //..some code here
}

But only when necessary and only if you are absolutely sure that this routine cannot be called from several separate threads simultaneously. (To make it multithread-compatible, you'll need some tricks - either mutexes, or the "pool" of several buffers)
3) Allocating more than 1 megabytes (for windows) or 8 megabytes (for linux) on stack will cause your program to crash (stack overflow for win, segfault on linux) unless you have specified stack size during compile time. Example:

void doStuff(){
    char buf[1024*1024*12];//use new[] or std::vector instead of this.
    //some code here
}

That's all I can think of.


Techniques for Optimization There are three types of techniques for optimizing code in C:

Compute-bound techniques Memory-bound techniques Input/Output-bound techniques

Compute-bound techniques involve computing the time taken to execute operators and functions. Compute-bound techniques include:

Profiling Inlining Loop unrolling Loop jamming Loop invariants computation Loop inversion Tail recursion elimination Table look up

In memory-bound computations, you need to take into account the memory that the program uses. The use of memory from the lower parts of the memory hierarchy increases the execution time. You need to use memory from the appropriate level while running programs. Memory-bound techniques include:

Locality of reference Row-Major addressing Padding reduction Memory leaks

In Input/Output (I/O) bound optimization, the sequential access and random access methods are used to reduce the time required to retrieve information.

A buffered I/O is faster than an unbuffered I/O. You can use read() and write() functions with large buffers. You can use mmap() to reduce time required to retrieve information. Mmap() maps the process’s address space with shared memory or file.

some more optimization techniques are :

Minimize the use of global variables in the program.

Declare all functions other than global variables as static within the file.

Use word size variables such as int and float instead of char, short, and double.

Avoid using recursion.

Avoid using the sqrt() function as it is CPU intensive.

Use single-dimensional arrays.

Do not split closely related functions into separate files.

Use puts() function instead of the printf function.

Use unformatted/binary file access instead of formatted file access.

Use mallopt() if the compiler supports this function to control malloc function.

Use macros instead of small functions to save CPU time.

0

精彩评论

暂无评论...
验证码 换一张
取 消