Online Book Reader

Home Category

Professional C__ - Marc Gregoire [417]

By Root 1434 0
function call would need a conditional statement to decide which function to call. If you don’t need those extra semantics, you can use a non-virtual function (although for safety and style reasons, we recommend you don’t). A general design rule in the C++ language is, “if you don’t use it, you don’t need to pay for it.” If you don’t use virtual methods, you pay no performance penalty for the fact that you could use them. Thus, non-virtual function calls in C++ are identical to function calls in C in terms of performance. However, since virtual function calls have such a tiny overhead, we recommend making all your class methods, including destructors but not constructors, virtual.

Also important, the high-level constructs of C++ enable you to write cleaner programs that are more efficient at the design level, are more easily maintained, and avoid accumulating unnecessary and dead code.

Finally, the authors of this book have used C++ for successful system-level software where high performance was required. We believe that you will be better served in your development, performance, and maintenance by choosing C++ instead of a procedural language.

LANGUAGE-LEVEL EFFICIENCY


Many books, articles, and programmers spend a lot of time trying to convince you to apply language-level optimizations to your code. These tips and tricks are important, and can speed up your programs in some cases. However, they are far less important than the overall design and algorithm choices in your program. You can pass-by-reference all you want, but it won’t ever make your program fast if you perform twice as many disk writes as you need. It’s easy to get bogged down in references and pointers and forget about the big picture.

Furthermore, some of these language-level tricks can be performed automatically by good optimizing compilers. Check your compiler documentation for details before spending time optimizing a particular area yourself.

In this book, we’ve tried to present a balance of strategies. Thus, we’ve included here what we feel are the most useful language-level optimizations. This list is not comprehensive, but should give you a good start if you want to optimize your code. However, make sure to read, and practice, the design-level efficiency advice described later in this chapter as well.

Apply language-level optimizations judiciously.

Handle Objects Efficiently

C++ does a lot of work for you behind the scenes, particularly with regard to objects. You should always be aware of the performance impact of the code you write. If you follow a few simple guidelines, your code will become significantly more efficient.

Pass-by-Reference

This rule is discussed elsewhere in this book, but it’s worth repeating here.

Objects should rarely be passed by value to a function or method.

Pass-by-value incurs copying costs that are avoided with pass-by-reference. One reason why this rule can be difficult to remember is that on the surface there doesn’t appear to be any problem when you pass-by-value. Consider a class to represent a person that looks like this:

class Person

{

public:

Person();

Person(const string& inFirstName, const string& inLastName, int inAge);

string getFirstName() const { return mFirstName; }

string getLastName() const { return mLastName; }

int getAge() const { return mAge; }

protected:

string mFirstName, mLastName;

int mAge;

};

Code snippet from Person\Person.cpp

You could write a function that takes a Person object as follows:

void processPerson(Person p)

{

// Process the person.

}

Code snippet from Person\Person.cpp

You might call it like this:

Person me("Marc", "Gregoire", 32);

processPerson(me);

Code snippet from Person\Person.cpp

This doesn’t look like there’s any more code than if you wrote the function like this instead:

void processPerson(const Person& p)

{

// Process the person.

}

Code snippet from Person\Person.cpp

The call to the function remains the same. However, consider what happens when you pass-by-value in the first version of the function. In order to

Return Main Page Previous Page Next Page

®Online Book Reader