Efficiency problem (reproduced) on I ++ and ++ i in C ++

xiaoxiao2021-03-06  41

A separate I or i effect is the same, and many books say that it is recommended to write into a form of i, but never explain the reason.

Why is it recommended to write i?

-------------------------------------------------- -------------

i ; // has a temporary variable

i; // no temporary variable

-------------------------------------------------- -------------

An article:

For (int i = 0; i <100; i )

{

/ * ............ * /

}

But few people think of hidden problems behind the code. People who have read the STL source code will be familiar with the code below.

Template inline

_Oi Copy (_ii _f, _ii _l, _oi _x)

{

For (; _f! = _l; _ x, _ f)

* _X = * _f;

Return (_X);

}

// Excerpt from Visual C 6.0 STL implementation, file: XUTILITY.

Why is it not written?

Template inline

_Oi Copy (_ii _f, _ii _l, _oi _x)

{for (; _f! = _l; _x , _f )

* _X = * _f;

Return (_X);

}

After reading this article, you can know the reason why you write.

C is a very powerful language that gives it the same power as his creator. The operations supported by each inherent data type can be implemented for types you define. The operator overload is an important part of it.

For a class CINT operator (here only discussed addition), we generally achieve this:

Class Cint

{

PUBLIC:

CINT & OPERATOR ();

CINT Operator (int);

CINT & OPERATOR = (const cint & That);

}

CINT Operator (Const Cint & this, Const Cint & That);

Where CINT & OPERATOR (); corresponds to i; (if i is an instance of CINT, the following) Returns the reason for returning a reference because in C , the result of i should be a left value. The call to this function, in addition to the operation itself, there is no overhead. (In addition to implied This, there is no passing parameters, there is only one reference return value, so there is no new instance being created)

CINT Operator (int); corresponding to i ; that int is meaningless, just to separate the prefix calculation and suffixal interfix. The reason for returning variables (rather than reference) is because the results of i should be a right value in C , and it is no previous value before the added (so it cannot return its const reference). In this function, a temporary variable is created and copied as a return value to the caller. CINT & Operator = (const cint & That) corresponding to i = i j; pass a parameter, in theory, its overhead and i's overhead are the same, but if you just want an instance of the class When adding one, i should be used because the function may be specially optimized. (Specific examples can be found)

Cint Operator (const cint & this, const cint & That); corresponding to K = I J; the overhead of this function is the same as i , but it should be noted that i may be optimized.

In order to be able to significantly measure the specific efficiency of each function, I use a very "big" class, CVector, a 65536 dimensional integer vector. The test results are:

Another = One: 4326

ANOTHER : 9274

Another = another one: 9223

Another: 2153

It can be seen that it is equally added, i can be much faster than i .

Of course, the optimization of the compiler is also important. In some cases, the compiler can optimize the speed of i to i. But why not write i directly? This ensures that faster execution speeds can be obtained in any case, rather than depends on the optimization of the compiler. (At least, Visual C 6.0 is not supported)

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

New Post(0)