More Effective C ++ Terms 23

zhaozj2021-02-08  264

Terms 23: Consider Change Base Library

The design of the library is a compromise process. The ideal library should be short, fast, powerful, flexible, scalable, intuitive, universally applicable, has good support, no constraints, no errors. This is not there. A library optimized for dimensions and speeds generally cannot be transplanted. A large number of functions will not be intuitive. Libraries without errors are limited in the range of use. In the real world, you can't have everything, there is always a payment.

Different designers give these conditions different priorities. They have sacrificed different things in the design. Therefore, two libraries that provide the same functionality have a completely different performance characteristics.

For example, considering the Iostream and STDIO libraries, both of them can be used for C programmers. Iostream libraries have several advantages over STDIO in C (see Effective C ). For example, it is type-safe (Type-Safe), which is scalable. However, in terms of efficiency, the iostream library is always less than stdio because the execution file generated by the STDIO is small and the execution file generated by Iostream is small.

First consider the problem of performing speed. To master the performance difference between Iostream and STDIO, a method is to use these two libraries to run the Benchmark program. But you must remember that Benchmark will also lie. It is not only difficult to take out a group of data that can represent the programs or programs, and it is useless to take it out, unless there is a reliable method to determine what kind of feature of your or your customers. However, in a comparison of a problem, Benchmark is still able to provide some information, so although it is completely relying on Benchmark, it is also stupid.

Let us test a simple Benchmark program that only tests the most basic I / O function. This program reads 30,000 floats from standard input, and then writes them in a fixed format to the standard output. The pre-processing symbol STDIO decided to use StDIO or iostream. If this symbol is defined, use stdio, otherwise you will use the iostream library.

#ifdef stdio

#include

#ELSE

#include

#include

Using namespace std;

#ENDIF

Const int values ​​= 30000; // # of values ​​to read / write

int main ()

{

Double D;

For (int N = 1; n <= values; n) {

#ifdef stdio

Scanf ("% lf", & d);

Printf ("% 10.5f", d);

#ELSE

Cin >> D;

COUT << SETW (10) // Set the Field Width

<< setPrecision (5) // Set the decimal location

<< setiosflags (ios :: showpoint) // Keep Trailing 0S

<< setiosflags (ios :: fixed) // Use these settings

<< d;

#ENDIF

IF (n% 5 == 0) {

#ifdef stdio

Printf ("/ n");

#ELSE

Cout << '/ n';

#ENDIF

}

}

Return 0;

}

When the nature of the positive integer is passed to this program, it will output this: 0.00000 0.69315 1.09861 1.38629 1.60944

1.79176 1.94591 2.07944 2.19722 2.30259

2.39790 2.48491 2.56495 2.63906 2.70805

2.77259 2.83321 2.89037 2.94444 2.99573

3.04452 3.09104 3.13549 3.17805 3.21888

This output indicates at least the use of iostreams, which can also produce Fixed-Format I / O. of course,

Cout << SETW (10)

<< setPRecision (5)

<< setiosflags (ios :: showpoint)

<< setiosflags (ios :: fixed)

<< d;

It's better than Printf ("% 10.5f", d); it is convenient to enter.

However, operators << are both type security (Type-Safe), and PrintF does not have these two advantages.

I have made several computers, operating systems, and compilers, running this program, in each case, running faster using stdio program. The advantage is just a few (about 20%), sometimes a lot (close to 200%), but I have never encountered an Iostream's implementation and the implementation of the corresponding STDIO's implementation is as fast as fast. In addition, the size of the program using STDIO is smaller than the corresponding procedure using iostream (sometimes much smaller). (This difference is negligible for the size of the program reality.

It should be noted that the efficiency of STDIO is mainly determined by its code, so the system I have tested or the current implementation of the system I have not tested may exhibit Iostream and STDIO did not have significant differences. In fact, there is reason to believe that an iostream code is faster than stdio because iostream determines the type of operand when compiling, and the STDIO function is to resolve the format string (Format string). The comparison between Iostream and STDIO is just an example. This is not important. It is important that different libraries with the same function take different trade-offs in performance, so once you find the bottleneck of the software (see Profile Terms 16), you should know if you may be able to eliminate the bottleneck by replacing the library. For example, if your program has I / O bottleneck, you can consider replacing iostream with stdio, if the program uses a lot of time on dynamic allocation and release memory, you can think about whether there are other Operator New and Operator Delete implementation available ( See Terms 8 and EFFECTIVE C Terms 10). Because different libraries contain different design concepts in efficiency, scalability, transplantability, type security, and other fields, you can sometimes increase software effectiveness.

转载请注明原文地址:https://www.9cbs.com/read-1004.html

New Post(0)