Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Professional C++ [eng].pdf
Скачиваний:
284
Добавлен:
16.08.2013
Размер:
11.09 Mб
Скачать

Writing Efficient C++

than just a function call: they also give you a run-time choice of which function to call. A comparable nonvirtual 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 nonvirtual 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, nonvirtual function calls in C++ are identical to function calls in C in terms of performance.

The critics might be right in one sense, however: some aspects of C++ make it easy to write inefficient code at the language-level. Using exceptions and virtual functions indiscriminately can slow down your program. However, this issue is insubstantial in light of the advantages C++ offers for your algorithms and overall design. 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, both authors of this book have used C++ for successful systems level software where highperformance 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 languagelevel 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.

467

Chapter 17

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

Pass-by-value incurs copying costs that are avoided by 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() { return firstName; }

string getLastName() { return lastName; } int getAge() { return age; }

private:

string firstName, lastName; int age;

};

You could write a function that takes a Person object in the following way:

void processPerson(Person p)

{

// Process the person.

}

You might call it like this:

Person me(“Nicholas”, “Solter”, 28);

processPerson(me);

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

void processPerson(const Person& p)

{

// Process the person.

}

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 initialize the p parameter of processPerson(), me must be copied with a call to its copy constructor. Even though you didn’t write a copy constructor for the Person class, the compiler generates one that copies each of the data members. That still doesn’t look so bad: there are only three data members. However, two of those are strings, which are themselves objects with copy constructors. So, each of their copy constructors will be called as well. The version of processPerson() that takes p by reference incurs no such copying costs. Thus, pass-by-reference in this example avoids three function calls when the code enters the function.

And you’re still not done. Remember that p in the first version of processPerson() is a local variable to the processPerson() function, and so must be destroyed when the function exits. This destruction requires a call to the Person destructor. Because you didn’t write a destructor, the default destructor

468

Writing Efficient C++

simply calls the destructor of all of the data members. strings have destructors, so exiting this function (if you passed by value) incurs calls to three destructors. None of those calls are needed if the Person object is passed by reference.

In summary, if a function must modify an object, you can simply pass the object by reference. If the function should not modify the object, you can pass it by const reference, as in the preceding example. See Chapter 12 for details on reference and const.

Return by Reference

Just as you should pass objects by reference to functions, you should also return them by reference from functions in order to avoid copying the objects unnecessarily. Unfortunately, it is sometimes impossible to return objects by reference, such as when you write overloaded operator+ and other similar operators. You should never return a reference or a pointer to a local object that will be destroyed when the function exits!

Catch Exceptions by Reference

As noted in Chapter 15, you should catch exceptions by reference in order to avoid an extra copy. As described later in this section, exceptions are heavy in terms of performance, so any little thing you can do to improve their efficiency will help.

Avoid Creating Temporary Objects

The compiler creates temporary, unnamed objects in several circumstances. Recall from Chapter 9 that after writing a global operator+ for a class, you can add objects of that class to other types, as long as those types can be converted to objects of that class. For example, the SpreadsheetCell class definition looks in part like the following:

class SpreadsheetCell

{

public:

// Other constructors omitted for brevity SpreadsheetCell(double initialValue);

friend const SpreadsheetCell operator+(const SpreadsheetCell& lhs,

const SpreadsheetCell& rhs);

// Remainder omitted for brevity

};

The constructor that takes a double allows you to write code like this:

SpreadsheetCell myCell(4), aThirdCell;

aThirdCell = myCell + 5.6; aThirdCell = myCell + 4;

The first addition line constructs a temporary SpreadsheetCell object from the 5.6 argument, then calls the operator+ with myCell and the temporary object as arguments. The result is stored in aThirdCell. The second addition line does the same thing, except that 4 must be coerced to a double in order to call the double constructor of the SpreadsheetCell.

469

Chapter 17

The important point in the above example is that the compiler generates code to create an extra, unnamed SpreadsheetCell object for each addition line. That object must be constructed and destructed with calls to its constructor and destructor. If you’re still skeptical, try inserting cout statements in your constructor and destructor and watching the printout.

In general, the compiler constructs a temporary object whenever your code converts a variable of one type to another type for use in a larger expression. This rule applies mostly to function calls. For example, suppose that you write a function with this signature:

void doSomething(const SpreadsheetCell& s);

You can call it like this:

doSomething(5.56);

The compiler constructs a temporary SpreadsheetCell object from 5.56 using the double constructor, which it passes to doSomething(). Note that if you remove the const from the s parameter, you can no longer call doSomething() with a constant: you must pass a variable. Temporary objects can only serve as targets of a const reference, not a non-const reference.

You should generally attempt to avoid cases in which the compiler is forced to construct temporary objects. Although it is impossible to avoid in some situations, you should at least be cognizant of the existence of this “feature” so you aren’t surprised by performance and profiling results.

The Return-Value Optimization

A function that returns an object by value can cause the creation of a temporary object. Continuing with the Person example, consider this function:

Person createPerson()

{

Person newP; return (newP);

}

Suppose that you call it like this (assuming that operator<< is implemented for the Person class):

cout << createPerson();

Even though this call does not store the result of createPerson() anywhere, the result must be stored somewhere in order to pass to the operator<< call. In order to generate code for this behavior, the compiler is allowed to create a temporary variable in which to store the Person object returned from createPerson().

Even if the result of the function is not used anywhere, the compiler might still generate code to create the temporary object. For example, suppose that you have this code:

createPerson();

The compiler might generate code to create a temporary object for the return value, even though it is not used.

470

Writing Efficient C++

However, you usually don’t need to worry about this issue because the compiler will optimize away the temporary variable in most cases. This optimization is called the return value optimization.

Don’t Overuse Costly Language Features

Several C++ features are costly in terms of execution speed: exceptions, virtual methods, and RTTI are the biggest offenders. If you are worried about efficiency, you should consider avoiding these features. Unfortunately, support for exceptions and RTTI incurs performance overhead even if you don’t explicitly use the features in your program. Support for only the possible use of those features requires extra steps during execution. Thus, many compilers allow you to specify that your program should be compiled without support for these features at all. For example, consider the following simple program that uses both exceptions and RTTI:

// test.cpp #include <iostream> #include <exception> using namespace std;

class base

{

public: base() {}

virtual ~base() {}

};

class derived : public base {};

int main(int argc, char** argv)

{

base* b = new derived();

derived* d = dynamic_cast<derived*>(b); // Use RTTI. if (d == NULL) {

throw exception(); // Use exceptions.

}

return (0);

}

Using g++ 3.2.2 on Linux, you can compile the program in the following way:

>g++ test.cpp

If you specify the g++ flag to disable exceptions, your attempt to compile looks like this:

>g++ -fno-exceptions test.cpp

test.cpp: In function `int main (int, char**)’:

test.cpp:20: exception handling disabled, use -fexceptions to enable

Strangely, if you specify the g++ flag to disable RTTI, the compiler successfully compiles the program, despite the obvious use of dynamic_cast:

>g++ -fno-rtti test.cpp

>

471