Two methods and efficiency analysis of value exchange

xiaoxiao2021-03-06  108

For example, we have two numbers A, B to exchange values, we generally achieve the goal in this way:

INT C;

C = a;

A = B;

B = C;

This will reach the purpose of exchange A, b value;

Users who often use the position, or there will be such a "high trick":

a ^ = b;

B ^ = a;

a ^ = b;

This also exchanges the value of A, B, and there is less an intermediate parameter.

Now we have seen two practices of our exchange value, which is better?

Oh, look at the reaction, we will result from each of the assembly, facts (time), etc.

C = B;

MOV EAX, DWORD PTR [EBP-8]

MOV DWORD PTR [EBP-0CH], EAX

B = a;

MOV ECX, DWORD PTR [EBP-4]

MOV DWORD PTR [EBP-8], ECX

A = C;

MOV EDX, DWORD PTR [EBP-0CH]

MOV DWORD PTR [EBP-4], EDX

There are six compilation instructions;

Let's take a look at the compilation instructions of the latter method:

a ^ = b;

MOV EAX, DWORD PTR [EBP-4]

XOR EAX, DWORD PTR [EBP-8]

MOV DWORD PTR [EBP-4], EAX

B ^ = a;

MOV ECX, DWORD PTR [EBP-8]

XOR ECX, DWORD PTR [EBP-4]

MOV DWORD PTR [EBP-8], ECX

a ^ = b;

MOV EDX, DWORD PTR [EBP-4]

XOR EDX, DWORD PTR [EBP-8]

MOV DWORD PTR [EBP-4], EDX

How? The latter method is more than three instructions than the previous method. Of course, more intuitive methods are tested, we can see the results of the contrast, and this result also reflects a relatively general Strategy.

the first method:

INT A = 9;

INT b = 8;

INT C;

Int ncount;

Ncount = gettickcount ();

Printf ("START TIME:% D / N", NCOUNT);

For (int i = 0; I <100000000; i )

{

C = a;

A = B;

B = C;

}

Ncount = gettickcount ();

Printf ("End Time:% D / N", NCOUNT);

turn out:

Start Time: 38830024

End Time: 38830395

Use 371 ms.

The second method:

INT A = 9;

INT b = 8;

Int ncount;

Ncount = gettickcount ();

Printf ("START TIME:% D / N", NCOUNT);

For (int i = 0; I <100000000; i )

{

a ^ = b;

B ^ = a;

a ^ = b;

}

Ncount = gettickcount ();

Printf ("End Time:% D / N", NCOUNT);

turn out:

Start Time: 38955024

End Time: 38956125

Used 1001ms.

The results of the two have a gap between orders under a large number of repetitive operations.

From both ways, we can see that although the first method time is small, it increases the consumption of a variable, although the second method is more, but saves a variable space, but its loss time is indeed It is very objective. In some occasions, the time is important, and the memory capacity is obviously not to be stretched, the first method is obviously suitable, it is actually a "space change time" strategy, and it is more It is easy to understand; and the second kind is not so easy (you may do a logical operation you can know if you do to exchange the value :), and its time loss is obvious (in the device that is more powerful, This is also the reason why this is also the second middle method almost quickly hidden. Once I saw someone in a forum, I saw the code of the second method, and said a wonderful three-line code, huh, huh.

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

New Post(0)