![]() |
| ||
| C++ Performance Tips Introduction These tips are based mainly on ideas from the book Efficient C++ by Dov Bulka and David Mayhew. For a more thorough treatment of performance programming with C++, I highly recommend this book. This document, while presenting many of the same ideas in the book, does not go into as much detail as to why certain techniques are better than others. The book also provides code examples to illustrate many of the points presented here. Constructors and Destructors
Methods that must return an object usually have to create an object to return. Since constructing this object takes time, we want to avoid it if possible. There are several ways to accomplish this.
Temporaries are objects that are "by-products" of a computation. They are not explicitly declared, and as their name implies, they are temporary. Still, you should know when the compiler is creating a temporary object because it is often possible to prevent this from happening.
Inlining is one of the easiest optimizations to use in C++ and it can result in the most dramatic improvements in execution speed. The main thing to know when using inlining is when you should inline a method and when you shouldn't inline.
|
| ||
| Re: C++ Performance Tips This is a short list of recommendations on how to use C++. My experiences are from using gcc 2.8.0 and Visual C++ 6.0. I had to have things compatible between these two compilers, and between Unix and Windows. (Note: The recommendations in this post may not be consistent with modern and correct C++ and were not peer reviewed before being posted to this thread. Use them with caution. -Narue) Contents IO of binary files When are destructors called for local variables Use {} to keep things local Scope of variables declared in for() When to use virtual members IO of binary files To make sure that there is no CR/LF translation on non-Unix computers, you have to use the following lines to open streams to files with binary data. For Visual C++, when using fstream.h, use in addition the flag ios::nocreate. Otherwise you can open a non-existing file for reading, without complaining. (This is not necessary when using fstream). When are destructors called for local variables Non-static (or 'automatic' ) variables are 'destructed' automatically when they go out of scope. Scope is a farily complicated thing, and I'm not going to repeat the definition here. Roughly speaking the scope ends when you encounter the } around the declaration of the variable. See also the use of {} and how scope is defined in the for() statement. Variables are destructed (by the compiler) by calling the appropriate destructor of their class. If the objects allocate memory (and hence the destructor should free that memory), this means that you recover the memory allocated. class array Use {} to keep things local Use of the grouping construct {} enables you to declare variables local to that group. When leaving the group, all local variables are destructed. This has the advantage that the reader of the code knows (s)he shouldn't worry about these variables to understand the rest of the code. In a way this can be understood as if every use of {} is like a function call (with local variables declared in the function). Of course, you don't have the overhead of stack manipulations and jumps involved in a proper function call. // recommended usage This tip is just an extension of the 'avoid global variables' credo. As always, this can be disabused as in the following piece of code, where the outer variable 'a' is hidden by a local 'a', resulting in not very readable code. // not very readable code Scope of variables declared in for() The new ANSI C++ standard specifies that variables declared as in for(int i=1; ...) have a scope local to the for statement. Unfortunately, older compilers (for instance Visual C++ 5.0) use the older concept that the scope is the enclosing group. Below I list 2 possible problems, and their recommended solutions:
When to use virtual members Make a member 'virtual' if a derived class extends the functionality of a member of the base class, and this extended functionality has to be accessible:
We wanted to have a 'grow' member that enlarged the outer dimension of the multidimensional array. At first sight, this is simply calling a general grow of the base class. However, 'grow' has to know the size of the new elements (which are again multidimensional arrays). So, we had to define in the derived class a new 'grow', which calls the base class 'grow' first, and then does more stuff. At many points in the base class, 'grow' is called to adjust sizes. By making 'grow' virtual we avoid to having to rewrite these members for the derived class. Caveat: For members of the base class which use temporary objects of its own type, the base class 'grow' will be called. For instance: class arrayThus, you should provide a member of the derived class for every member of the base class which uses temporary objects. class multiarray : public array |
| ||
| Re: C++ Performance Tips I Dont agree for usage of virtual functions neagtively impacts the performance..it does..but one needs to pay price for sth he/she wants to use..If one wants to implement the functionality of virtual functions then he/she would have to go thru technique like vtable. Advantages of virtual functions - Readablity - No Need to change existing code for incrementing functionalities in terms of inheritance - Better than type field solution. - and many more.. All above from bjarne Stroustrup. The C++ programming languages..inventor of C++ Happy going virtual!!!!!!!!!!! Quote:
|
| ||
| Re: C++ Performance Tips When you want to pass a constant variable, pass it by reference to save memory. Example: int example (const int value); // Uses more memory than int example (const int &value); // this one |
| ||
| Re: C++ Performance Tips How about precalculated values. It saves a lot of time in slower machines during long iterations, does it not? For eg: x=23.45; xinv=1/x; and use xinv where its needed. Also one can use precalculated trigonometrical functional values such as float x=sin(x); and so on. |
| ||
| Re: C++ Performance Tips And the number one rules of optimization is??? DO NOT (Atleast not untill you _know_ you need to, and then only where you _know_ it will make a difference, eg profiling). On a side not I recently tried some wtl and they have a rather nifty technique for avoiding virtual functions while still allowing inheritance (does not work well all the time since only a limited amount of the behaviour is simulated though)... well anyway if you are intrested take a look (it involves specifying the extending class as a template argument to the base class so the implemented class is known at compile time)... |
| ||
| Re: C++ Performance Tips Two guidelines from Thinking in C++ by Bruce Eckel:
See book: http://www.pythoncriticalmass.com/ |
| ||
| Re: C++ Performance Tips An easy way to swap 2 variables without using another variable: a=a+b; |
| ||
| Re: C++ Performance Tips Quote:
Don't do this today or in the future. Use a temp variable. |
| ||
| Re: C++ Performance Tips Quote:
and you don't have to use another variable, in C++ standard, there is the STL. It is the strength of C++ because there are lots and lots of algorithms in it. For example, there is a sort that sorts your elements always in O(n log n) time, which is the fastest it can get (if you don't count counting sort). Therefore I use the it any time I can. There is also a templated function swap, that swaps any two elements that are of same type. So i think it is be easier and cleverer to type swap(a, b); than to use your idea. |
| All times are GMT -4. The time now is 5:42 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC