5 Simple C++ optimization techniques

October 7, 2008 at 4:13 pm Leave a comment

We should generally not bother optimizing our code until we know where to optimize. We should first know where the processor is spending most if its time and then figure out how to optimize the code rather than guessing where to apply it. The flip side of optimization is that it makes the code less readable and we all know that developer’s time is more valuable than the machine’s time. Apart from writing code, someone has to maintain it and therefore one should write extensive comments where ever the code is less readable. But there are some simple optimization techniques that can easily applied be applied while writing the code itself.

1. Pass class parameters by reference

void PassByReference(const V& v)

When an object is passed by value, the entire object gets copied by calling copy constructor. But when the object passed by reference the only fine we pay is when the object is de-referenced while being used within the function. Built-in types (int, float etc) should be passed by value though, because its cheap to copy them.

A lot of us don’t pass strings and container objects by reference and this can sometime prove very coslty.

2. Constructor initialization list

Before entering into the body of the constructor of a class, its member variables are automatically constructed using their default constructors. When this member variable is assigned to some value in the body of the constructor the assignment operator is invoked. Which means, the member variables are effectively being created twice.

Foo::Foo(const V& v)//m_var's default
                    //constructor is called
    m_var = v;//m_var's assignment operator is called

When initialization list is used there is only one call.

Foo::Foo(const V& v) : m_var(v) //m_var's copy
                                //constructor is called.
{ }

However, the draw back when using initialization list is that error checking cannot be done on these values. Also, if someone is not used to such code there can be some readability issues.

3. Use operator=

For classes where construction time is costly one should use operator= ( += ) rather than only operator ( + ). When only operator is used a temporary object has to be created and destroyed. One can save this time by modifying the existing object itself.

Foo(V& a, V& b)
    a += b; //Instead of a = a + b;

4. Minimize the scope of a variable

Creating a non-trivial object can be expensive because there is some construction time involved. Unlike in C where one has to declare all the variables at the starting of a function, in C++ variables can be declared just before using it.

    V a; //a is constructed even when
         //the condition is false
    if(b) {
        a = t;
        //Do something

The above code can be re-written as

    if(b) {
        V a = t; //Construct a only if its required.
        //Do something

5. Initialize on declaration

Defining an object and then assigning a value to it invokes both constructor and assignment operator. Whereas, initializing an object invokes only copy constructor. Where ever possible postpone your declaration till you can initialize it.

    V x;//Default constructor
    x = t;//Assignment operator
    V x = t; //Initialization

One other advantage of this technique is that it improves readability.


Entry filed under: Geeky stuff. Tags: .

The Ferrari products 5 simple ways to become a successful freelancer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


  • RT @alexanderliegl: In the future, the ability to concentrate will be a competitive advantage. 2 weeks ago
  • RT @beeonaposy: Jumping back into code you wrote ages ago like 10% luck 20% skill 15% concentrated power of will 5% pleasure… 1 month ago
  • RT @jimmysong: Stuff CS departments should teach, but don't do very well: * Source control, esp. git * How to do effective code review * H… 1 month ago


%d bloggers like this: