循环效率的c ++:身体与事后的想法

时间:2016-07-18 05:37:46

标签: c++ for-loop

我在leetcode上找到了一些有趣的东西,希望有人能帮助解释原因:

我基本上是在进行合并排序,并使用快速慢速指针来查找中间指针。以下是此类代码段的两个版本:

1。事后更新

    for (ListNode* fast=head; 
         fast->next && fast->next->next; 
         fast = fast->next->next, slow = slow->next) { }

2。正文更新

    for (ListNode* fast=head; fast->next && fast->next->next; ) {
        fast = fast->next->next;
        slow = slow->next;
    }

为什么版本2比第一版快?

编译器:g ++ 4.9.2

1 个答案:

答案 0 :(得分:4)

逗号操作不太可能显着降低for循环的速度。

我已经制作了两个变体并打开了反汇编(在Visual Studio 2012中),以便他们看到差异。

  1. 看起来像:

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                    // Kill your service here
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(10);
                }
            });
    
  2. 是:

        for (ListNode* fast = head;
    0022545E  mov         eax,dword ptr [head]  
    00225461  mov         dword ptr [ebp-2Ch],eax  
            fast->next && fast->next->next;
    00225464  jmp         main+17Bh (022547Bh)  
            fast = fast->next->next, slow = slow->next) {
    00225466  mov         eax,dword ptr [ebp-2Ch]  
    00225469  mov         ecx,dword ptr [eax+4]  
    0022546C  mov         edx,dword ptr [ecx+4]  
    0022546F  mov         dword ptr [ebp-2Ch],edx  
    00225472  mov         eax,dword ptr [slow]  
    00225475  mov         ecx,dword ptr [eax+4]  
    00225478  mov         dword ptr [slow],ecx  
    0022547B  mov         eax,dword ptr [ebp-2Ch]  
    0022547E  cmp         dword ptr [eax+4],0  
    00225482  je          main+192h (0225492h)  
    00225484  mov         eax,dword ptr [ebp-2Ch]  
    00225487  mov         ecx,dword ptr [eax+4]  
    0022548A  cmp         dword ptr [ecx+4],0  
    0022548E  je          main+192h (0225492h)  
        }
    
  3. 只有一个 for (ListNode* fast = head; fast->next && fast->next->next;) { 0024545E mov eax,dword ptr [head] 00245461 mov dword ptr [ebp-2Ch],eax 00245464 mov eax,dword ptr [ebp-2Ch] 00245467 cmp dword ptr [eax+4],0 0024546B je main+190h (0245490h) 0024546D mov eax,dword ptr [ebp-2Ch] 00245470 mov ecx,dword ptr [eax+4] 00245473 cmp dword ptr [ecx+4],0 00245477 je main+190h (0245490h) fast = fast->next->next; 00245479 mov eax,dword ptr [ebp-2Ch] 0024547C mov ecx,dword ptr [eax+4] 0024547F mov edx,dword ptr [ecx+4] 00245482 mov dword ptr [ebp-2Ch],edx slow = slow->next; 00245485 mov eax,dword ptr [slow] 00245488 mov ecx,dword ptr [eax+4] 0024548B mov dword ptr [slow],ecx } 是区别。 对不起,但是我看不出重大的差异,所以也许性能问题并不代替那两个陈述。