If-else Three mesh operator bottom implementation efficiency difference

Source: Internet
Author: User

A summary of your own after reading the article:
the three-mesh operator is faster than if-else in the absence of compiler optimizations, because the three-mesh operator also uses an extra temporary variable, which first operations and then replicates, If-else is directly assigned, so the speed is fast, But now the compiler has to do these optimizations, the optimized assembly code is the same, just like i++ and ++i, compile the optimized assembly code is the same.
Original address:
Http://blog.sina.com.cn/s/blog_6cf502aa0101e7mx.html#post
Original content:
One day a classmate is doing algorithm problem, (travel backpack) Timeout in OJ system submission when we try to optimize the code to reduce running time, we find that seemingly if-else is more than three mesh operations?: faster. So, VC wrote a short test. It is also a double loop that performs if-else and three-mesh operations to calculate the time it takes.

A=100,b=50,n is the test variable 
start=clock ();
for (i=1;i<=n;i++)
{for
      (j=1;j<=n;j++)
     {
           if (a>b)
             temp=a;
           else
             temp=b;
      }
}
End=clock ();
cout << "time=" << end-start << Endl;
for (i=1;i<=n;i++)
{for
       (j=1;j<=n;j++)
       {
            temp=a>b?a:b;
        }
}
 } Here is the test result:
 The time unit is Ms
 n=100 if time
 = 0 The
 :. Time = 0

 n=1000 If time = 2 The
 :? 4

 n=10,000  
 If time = 253 the
 :. Time = 360

 n=50,000  
 If time = 6209 the
 :. Time = 8941

 n =100,000   
 If time = 24343 the
 :. Time = 35807

You can see, as the n gets bigger, the gap between the two is becoming more and more obvious (not many computer tests, only through their own test, the magic is not much difference in Ubuntu, the problem is said later), and then I compile, look at their assembly code, found a mysterious thing I do not understand.

The following are the assembly codes for the If-else and three mesh operations respectively.

37:if (a>b) 00401079 mov ecx,dword ptr [ebp-10h] 0040107C cmp ecx,dword ptr [ebp-14h]
0040107F jle main+79h (00401089) 38:temp=a; 00401081 mov edx,dword ptr [ebp-10h] 00401084 mov dword ptr [Ebp-18h],edx 39:else 00401
087 jmp main+7fh (0040108f) 40:temp=b; 00401089 mov eax,dword ptr [ebp-14h] 0040108C mov dword ptr [Ebp-18h],eax 51:TEMP=A&G
T;B?A:B; 004010F3 mov edx,dword ptr [ebp-10h] 004010f6 cmp edx,dword ptr [ebp-14h] 004010f9 Jle 0F3H (00401103) 004010FB mov eax,dword ptr [ebp-10h] 004010FE mov dword ptr [Ebp-24h],eax 00401101 J MP main+0f9h (00401109) 00401103 mov ecx,dword ptr [ebp-14h] 00401106 mov dword ptr [ebp-24h],
 ECX 00401109 mov edx,dword ptr [ebp-24h] 0040110C mov dword ptr [Ebp-18h],edx

It can be seen that the latter has two more instructions than the former one.
If-else in either case (in if or else), you pass the variable value that you want to assign to the register and then assign the value to the TEMP variable by register. That
mov edx,b; mov temp,edx;
However, for the three-mesh operation, one of its steps adds a temporary variable.
mov Ecx,b;mov newtemp,ecx;
mov Edx,newtemp;mov Temp;edx;
Here is a classmate to give me the answer (he learned from the principle of compiling):
Because the three-mesh operation is the first operation, and then assign the value.
For example:
temp = a > B? A:B;
a > B? A:B is the operation, temp = (a > b?). A:B) is a value assignment.
So in the three-purpose assembly inside A,b,temp address for 10h,14h 18h no matter how the results of a>b, there are the results of the following is stored in the middle variable address 24h, and then assigned to the address 18h of the TEMP variable.

The IF statement is directly assigned, there is no operation, so a little faster.

Then according to this principle, you can know ++i faster than i++, x+=y faster than X=x+y.
However, we can ignore them when we write the program, because the compiler has helped us optimize ...
So whether you write i++ or ++i, it's the same, and here's the proof.
00401048 mov dword ptr [ebp-4],1
32:
: ++i;
0040104F mov eax,dword ptr [ebp-4]
00401052 Add eax,1
00401055 mov dword ptr [Ebp-4],eax
34:
35:i++;
00401058 mov ecx,dword ptr [ebp-4]
0040105B Add ecx,1
0040105E mov dword ptr [EBP-4],ECX

But why the VC did not optimize the three-mesh operation. Change it to an if form.
Back to the problems left above, why is running in Ubuntu no different. --> Perhaps the compiler in Ubuntu has optimized it
Before in Baidu to find the answer, that is, if the value of three is changed to a constant, such words do not need to be like if-else to jump, conducive to CPU implementation. (This involves hardware, I do not understand, also do not have the interest to continue to follow the question ~ This problem, to end it.) )

The following is the assembly code for the constants. There is no jump instruction to change 1:0 to other numbers, the test is not, although the instructions will be a few more.
38:z=x>y?1:0;
0040106E mov ecx,dword ptr [ebp-8]
00401071 XOR Edx,edx
00401073 cmp Ecx,dword ptr [ebp-0ch]
00401076 SETG DL
00401079 mov dword ptr [Ebp-4],edx

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.