Useless Computations

Lazy Evaluation is about computations that are not always necessary, depending on execution flow. Useless computations are about those computations that are never necessary. These are entirely pointless computations whose results are never used regardless of execution flow.

A subtle example of a useless computation is the wasted initialization of a member object.

class Student {
public:
    Student(char *nm);
     ...
private:
    string name;
};

The Student constructor turns the input character pointer into a string object representing the student's name:

Student::Student(char *nm)
{
    name = nm;
    ...
}

C++ guarantees that all member objects have already been constructed before the body of the Student constructor is executed. In our ...

Get Efficient C++ Performance Programming Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.