•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,471 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,972 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 37297 | Replies: 40
![]() |
•
•
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation:
Rep Power: 5
Solved Threads: 2
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.
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
- The performance of constructors and destructors is often poor due to the fact that an object's constructor (destructor) may call the constructors (destructors) of member objects and parent objects. This can result in constructors (destructors) that take a long time to execute, especially with objects in complex hierarchies or objects that contain several member objects. As long as all of the computations are necessary, then there isn't really a way around this. As a programmer, you should at least be aware of this "silent execution". If all of the computations mentioned above are not necessary, then they should be avoided. This seems like an obvious statement, but you should be sure that the computations performed by the constructor that you are using is doing only what you need.
- Objects should be only created when they are used. A good technique is to put off object creation to the scope in which it is used. This prevents unnecessary constructors and destructors from being called.
- Using the initializer list functionality that C++ offers is very important for efficiency. All member objects that are not in the initializer list are by default created by the compiler using their respective default constructors. By calling an object's constructor in the initializer list, you avoid having to call an object's default constructor and the overhead from an assignment operator inside the constructor. Also, using the initializer list may reduce the number of temporaries needed to construct the object. See the Temporaries section for more information on this.
- Virtual functions negatively affect performance in 3 main ways:
- The constructor of an object containing virtual functions must initialize the vptr table, which is the table of pointers to its member functions.
- Virtual functions are called using pointer indirection, which results in a few extra instructions per method invocation as compared to a non-virtual method invocation.
- Virtual functions whose resolution is only known at run-time cannot be inlined. (For more on inlining, see the Inlining section.
- Templates can be used to avoid the overhead of virtual functions by using a templated class in place of inheritance. A templated class does not use the vptr table because the type of class is known at compile-time instead of having to be determined at run-time. Also, the non-virtual methods in a templated class can be inlined.
- The cost of using virtual functions is usually not a factor in calling methods that take a long time to execute since the call overhead is dominated by the method itself. In smaller methods, for example accessor methods, the cost of virtual functions is more important.
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.
- Instead of returning an object, add another parameter to the method which allows the programmer to pass in the object in which the programmer wants the result stored. This way the method won't have to create an extra object. It will simply use the parameter passed to the method. This technique is called Return Value Optimization (RVO).
- Whether or not RVO will result in an actual optimization is up to the compiler. Different compilers handle this differently. One way to help the compiler is to use a computational constructor. A computational constructor can be used in place of a method that returns an object. The computational constructor takes the same parameters as the method to be optimized, but instead of returning an object based on the parameters, it initializes itself based on the values of the parameters.
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.
- The most common place for temporaries to occur is in passing an object to a method by value. The formal argument is created on the stack. This can be prevented by using pass by address or pass by reference.
- Compilers may create a temporary object in assignment of an object. For example, a constructor that takes an int as an argument may be assigned an int. The compiler will create a temporary object using the int as the parameter and then call the assignment operator on the object. You can prevent the compiler from doing this behind your back by using the explicit keyword in the declaration of the constructor.
- When objects are returned by value, temporaries are often used. See the Return Value section for more on this.
- Temporaries can be avoided by using <op>= operators. For example, the code
a = b + c;
could be written as
a=b;
a+=c;.
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.
- There is always a trade-off between code size and execution speed when inlining. In general, small methods (for example, accessors) should be inlined and large methods should not be inlined.
- If you are not sure of whether or not a given method should be inlined, the best way to decide is to profile the code. That is, run test samples of the code, timing inlining and non-inlining versions.
- Excessive inlining can drastically increase code size, which can result in increased execution times because of a resulting lower cache hit rate.
- Watch out for inlined methods that make calls to other inlined methods. This can make the code size unexpectedly larger.
- Singleton methods, methods that are only called from one place in a program, are ideal for inlining. The code size does not get any bigger and execution speed only gets better.
- Using literal arguments with an inlined method allows the compiler to make significant optimizations. (This is, however, compiler dependent.)
- The compiler preprocessor can be used to implement conditional inlining. This is useful so that during testing the code is easier to debug. But for compiling production code, there are no changes to be made to the source code. This is implemented by using a preprocessor macro called INLINE. Inlined code is defined within #ifdef INLINE ... #endif code blocks. Similarly, non-inlined code is defined within #ifndef INLINE ... #endif code blocks. Then to compile using inlined code, you tell the compiler to treat INLINE as defined. (-DINLINE with g++)
- Sometimes it makes sense to inline a given method in some places, but to not inline in other places within the same program. This can be accomplished using a technique called selective inlining. The implementation of this technique is not very convenient. For each method that you want to selectively inline you have two methods, where on one has "inline_" prepended to the method name and is of course inlined. The method without the "inline_" prepended to its name simply calls the inlined version of the method.
- Recursive calls cannot be inlined, but there are two techniques to try to improve performance in the case of recursive methods:
- If the recursion is tail recursion, then the algorithm can be rewritten as an iterative algorithm, eliminating the overhead of method invocations.
- Recursive call unrolling basically allows the programmer to inline the first steps of recursion in a recursive algorithm. For example, for a recursive method print() we might do the following: print_unrolled() calls print1() which calls print2() which calls print3() which calls print(). All methods except print() are inlined. The number of recursive steps can be made as high as desired, depending on the application.
Real Eyes Realize Real Lies
•
•
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation:
Rep Power: 5
Solved Threads: 2
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.
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.
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.
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:
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:
Thus, you should provide a member of the derived class for every member of the base class which uses temporary objects.
(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.
ofstream os("output.flt", ios::out | ios::binary);
ifstream is("output.flt", ios::in | ios::binary);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
{
private:
float *ptr;
public:
// constructor
array(int n) { ptr = new float[n]; }
// destructor
~array() { delete [] ptr; }
}
main()
{
// ...
{
array a(5); // allocates memory
// do something
}
// here the array is destructed, and so the memory is freed
//....
}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
void f(int a)
{
if(a==1)
{
myclassA Aobject;
// here I do something with 'Aobject', and maybe 'b'
}
// Aobject does not exist here anymoreThis 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
{
int a=1;
{
// local variable hides outer 'a'
int a;
a = 2;
assert (a==2);
}
// a is again the previous variable
assert (a==1);
}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:
- you want to use the variable after the for() statement
- you have to declare the variable outside of the for() statement.
When to use virtual members
int i; for(i=1; i<5; i++) { /* do something */ } if (i==5) ...
- you want to have multiple for() loops with the same variables.
Put the for statement in its own group. You could also declare the variable outside of the 'for', but it makes it slightly trickier for an optimising compiler (and a human) to know what you intend.
{ for(i=1; i<5; i++) { /* do something */ } }
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:
- inside other member functions of the base class
- when using pointers that can point to either an object of the base class, or an object of the derived class.
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 array
{
...
virtual void grow(int new_size);
array& operator +=( const array& a)
{ /* some definition using 'grow' */ }
array operator +(const array& a1, const array& a2)
{
array a=a1;
a += a2;
// Warning, this will call array::grow, even if a1 is really from a derived type
}
};
Thus, you should provide a member of the derived class for every member of the base class which uses temporary objects.
class multiarray : public array { ... virtual void grow(int new_size); multiarray operator +(const multiarray& a1, const multiarray& a2) { multiarray a=a1; a += a2; } };
Last edited by Narue : Apr 7th, 2005 at 4:30 pm. Reason: This post was a separate thread that I intentionally chose not to sticky. A suitable warning has been added.
Real Eyes Realize Real Lies
•
•
Join Date: May 2005
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 1
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!!!!!!!!!!!
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!!!!!!!!!!!
•
•
•
•
Originally Posted by meabed
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 DestructorsVirtual Functions
- The performance of constructors and destructors is often poor due to the fact that an object's constructor (destructor) may call the constructors (destructors) of member objects and parent objects. This can result in constructors (destructors) that take a long time to execute, especially with objects in complex hierarchies or objects that contain several member objects. As long as all of the computations are necessary, then there isn't really a way around this. As a programmer, you should at least be aware of this "silent execution". If all of the computations mentioned above are not necessary, then they should be avoided. This seems like an obvious statement, but you should be sure that the computations performed by the constructor that you are using is doing only what you need.
- Objects should be only created when they are used. A good technique is to put off object creation to the scope in which it is used. This prevents unnecessary constructors and destructors from being called.
- Using the initializer list functionality that C++ offers is very important for efficiency. All member objects that are not in the initializer list are by default created by the compiler using their respective default constructors. By calling an object's constructor in the initializer list, you avoid having to call an object's default constructor and the overhead from an assignment operator inside the constructor. Also, using the initializer list may reduce the number of temporaries needed to construct the object. See the Temporaries section for more information on this.
Return Value
- Virtual functions negatively affect performance in 3 main ways:
- The constructor of an object containing virtual functions must initialize the vptr table, which is the table of pointers to its member functions.
- Virtual functions are called using pointer indirection, which results in a few extra instructions per method invocation as compared to a non-virtual method invocation.
- Virtual functions whose resolution is only known at run-time cannot be inlined. (For more on inlining, see the Inlining section.
- Templates can be used to avoid the overhead of virtual functions by using a templated class in place of inheritance. A templated class does not use the vptr table because the type of class is known at compile-time instead of having to be determined at run-time. Also, the non-virtual methods in a templated class can be inlined.
- The cost of using virtual functions is usually not a factor in calling methods that take a long time to execute since the call overhead is dominated by the method itself. In smaller methods, for example accessor methods, the cost of virtual functions is more important.
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
- Instead of returning an object, add another parameter to the method which allows the programmer to pass in the object in which the programmer wants the result stored. This way the method won't have to create an extra object. It will simply use the parameter passed to the method. This technique is called Return Value Optimization (RVO).
- Whether or not RVO will result in an actual optimization is up to the compiler. Different compilers handle this differently. One way to help the compiler is to use a computational constructor. A computational constructor can be used in place of a method that returns an object. The computational constructor takes the same parameters as the method to be optimized, but instead of returning an object based on the parameters, it initializes itself based on the values of the parameters.
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
- The most common place for temporaries to occur is in passing an object to a method by value. The formal argument is created on the stack. This can be prevented by using pass by address or pass by reference.
- Compilers may create a temporary object in assignment of an object. For example, a constructor that takes an int as an argument may be assigned an int. The compiler will create a temporary object using the int as the parameter and then call the assignment operator on the object. You can prevent the compiler from doing this behind your back by using the explicit keyword in the declaration of the constructor.
- When objects are returned by value, temporaries are often used. See the Return Value section for more on this.
- Temporaries can be avoided by using <op>= operators. For example, the code
a = b + c;
could be written as
a=b;
a+=c;.
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.
- There is always a trade-off between code size and execution speed when inlining. In general, small methods (for example, accessors) should be inlined and large methods should not be inlined.
- If you are not sure of whether or not a given method should be inlined, the best way to decide is to profile the code. That is, run test samples of the code, timing inlining and non-inlining versions.
- Excessive inlining can drastically increase code size, which can result in increased execution times because of a resulting lower cache hit rate.
- Watch out for inlined methods that make calls to other inlined methods. This can make the code size unexpectedly larger.
- Singleton methods, methods that are only called from one place in a program, are ideal for inlining. The code size does not get any bigger and execution speed only gets better.
- Using literal arguments with an inlined method allows the compiler to make significant optimizations. (This is, however, compiler dependent.)
- The compiler preprocessor can be used to implement conditional inlining. This is useful so that during testing the code is easier to debug. But for compiling production code, there are no changes to be made to the source code. This is implemented by using a preprocessor macro called INLINE. Inlined code is defined within #ifdef INLINE ... #endif code blocks. Similarly, non-inlined code is defined within #ifndef INLINE ... #endif code blocks. Then to compile using inlined code, you tell the compiler to treat INLINE as defined. (-DINLINE with g++)
- Sometimes it makes sense to inline a given method in some places, but to not inline in other places within the same program. This can be accomplished using a technique called selective inlining. The implementation of this technique is not very convenient. For each method that you want to selectively inline you have two methods, where on one has "inline_" prepended to the method name and is of course inlined. The method without the "inline_" prepended to its name simply calls the inlined version of the method.
- Recursive calls cannot be inlined, but there are two techniques to try to improve performance in the case of recursive methods:
- If the recursion is tail recursion, then the algorithm can be rewritten as an iterative algorithm, eliminating the overhead of method invocations.
- Recursive call unrolling basically allows the programmer to inline the first steps of recursion in a recursive algorithm. For example, for a recursive method print() we might do the following: print_unrolled() calls print1() which calls print2() which calls print3() which calls print(). All methods except print() are inlined. The number of recursive steps can be made as high as desired, depending on the application.
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)...
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)...
•
•
Join Date: Sep 2005
Location: Chisinau, Rep. of Moldova
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Two guidelines from Thinking in C++ by Bruce Eckel:
See book: http://www.pythoncriticalmass.com/
- Avoid the preprocessor. Always use const for value substitution and inlines for macros.
- Avoid global variables. Always strive to put data inside classes. Global functions are more likely to occur naturally than global variables, although you may later discover that a global function may fit better as a static member of a class.
See book: http://www.pythoncriticalmass.com/
•
•
Join Date: Sep 2005
Location: Chisinau, Rep. of Moldova
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
An easy way to swap 2 variables without using another variable:
a=a+b; b=a-b; a=a-b;
•
•
•
•
Originally Posted by bitforce
An easy way to swap 2 variables without using another variable:
a=a+b; b=a-b; a=a-b;
Don't do this today or in the future. Use a temp variable.
•
•
Join Date: Mar 2006
Location: Zagreb, Croatia
Posts: 18
Reputation:
Rep Power: 3
Solved Threads: 0
•
•
•
•
Originally Posted by bitforce
An easy way to swap 2 variables without using another variable:
a=a+b; b=a-b; a=a-b;
it can't be used if operator+ and operator- arn't defined!!!
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. Revenage is a dish best served cold.
50|2|2Y 4 |34|) 3|\|6|_|5|-| ![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- improve performance of the following io codes (C)
- Performance Improvements (Windows NT / 2000 / XP / 2003)
Other Threads in the C++ Forum
- Previous Thread: 8 Queens Problem - Segmentation Fault
- Next Thread: references to local objects



Linear Mode