Two Methods of value exchange and Efficiency Analysis

Source: Internet
Author: User

For example, we have two numbers A and B to exchange values. We generally achieve the goal through this method:

Int C;

C =;

A = B;

B = C;

In this way, the and B values are exchanged;

People who often use bit operations may have such "tricks ":

A ^ = B;

B ^ =;

A ^ = B;

In this way, the values of A and B can be exchanged, and an intermediate parameter is used less.

Now we can see two ways of exchanging values. Which one is better?

Haha, look at the response. We will get the result from the perspectives of compilation and fact (time.

C = B;

MoV eax, dword ptr [ebp-8]

MoV dword ptr [ebp-0Ch], eax

B =;

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 a total of six Assembly commands;

Let's look at the number of Assembly commands in the last method:

A ^ = B;

MoV eax, dword ptr [ebp-4]

XOR eax, dword ptr [ebp-8]

MoV dword ptr [ebp-4], eax

B ^ =;

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 is it? The latter method has three more commands than the former method. of course, the more intuitive method is to test it. We can see the comparison result more clearly, and this result also reflects a general strategy.

Method 1:

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 = B;

B = C;

}

Ncount = gettickcount ();

Printf ("End Time: % d/N", ncount );

The result is:

Starting Time: 38830024

Endtime: 38830395

371 Ms.

 

Method 2:

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 ^ = B;

}

Ncount = gettickcount ();

Printf ("End Time: % d/N", ncount );

The result is:

Starting Time: 38955024

Endtime: 38956125

1001 Ms.

The results of the two operations differ by an order of magnitude in a large number of repeated operations.

From the two methods, we can see that although the first method is less time-consuming, it increases the consumption of a variable. The second method, although time-consuming, saves space for a variable, however, the time efficiency of the loss is objective.

In some cases, time is very important, and the memory capacity is obviously not too slow, the first method is obviously appropriate, it is actually a reflection of a "space for Time" strategy, and it is easier to understand. The second approach is not that easy to understand (you may have to perform a logical operation to know that they have indeed exchanged values :), in addition, the time loss is obvious (this is intolerable among devices with extremely high power consumption ). this is why the second method almost disappears. I once saw someone come up with the code for the second method in a forum and said it was a wonderful three-line code.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.