More Effective C study notes four: Efficiency

1. Understanding 80-20 Guidelines

2. Consider using lazy evaluation method

For example reference counting approach the string class

Distinguish between read and write

Lazy extract: pointer to implemention of the design ideas

Lazy expression evaluation

3. Calculate the desired amortization

Ahead of time-sharing computing consumption is above a contrary idea, over-enthusiastic calculation method

For example, using the cache (considered in the calculation, or more frequently the same operation is repeated)

.vector container memory expansion logic

4. Understand the source of temporary objects

The establishment of a no-name non-heap objects will produce temporary objects, you can create, for example, typename (...) Manual;

More generally, implicit conversion is generated in the function call is generated or when the function returns.

Const reference no temporary objects (function call)

 

5. Assist return value optimization

Returned object is inevitable.

You should consider reducing the cost of returns objects rather than avoid returning objects.

It returns the object in some way that allows the compiler to eliminate the overhead of temporary objects.

That return constructor argument rather than a direct return to the object.

const Rational operator*(const Rational& lhs, const Rational& rhs)
{
    return Rational(lhs.numerator() * rhs.numerator(), lhs.denominator() + rhs.denominator());
}

The establishment of a temporary object returned. Constructor and destructor consumption inside this method still has a function of temporary object, returns object constructors and destructors consumption will have.

Benefit is c ++ rules allow the compiler to optimize temporary objects does not appear, as follows:

Rational a = 10;

Rational b(1, 2);

Rational c = a * b;

Like to call the compiler will be allowed to eliminate temporary variables within the operator * and operator * temporary variables returned, leaving only a function of the cost structure, that structure C.

c can not be eliminated, because c is a named object, named object can not be eliminated. In addition to this function you can also declared inline to eliminate its call overhead.

6. By avoiding overloading implicit type conversions

7. Consider alone form a substituted operator assignment form (op =)

8. consider changing library

9. appreciated virtual functions, multiple inheritance, and virtual base classes cost RTTI

Learn virtual table pointer and the virtual table, i.e. vtbl and the vptr. Vtbl is usually a function of a pointer array (or list). Declared in the class or inherited virtual functions, have their own

vtbl, vtbl project implementation pointer is a pointer to the virtual function member.

The cost required for the virtual function call calling a function substantially the same as through a function pointer, the virtual function itself is generally not a bottleneck in performance.

Virtual functions can not be inlined,

 

 

 

 

 

 

 

 

 

 

Published 44 original articles · won praise 5 · Views 1396

Guess you like

Origin blog.csdn.net/qq_33776188/article/details/104635658