簡體   English   中英

在for循環中避免if語句?

[英]Avoiding if statement inside a for loop?

我有一個名為Writer的類,它有一個函數writeVector如下所示:

void Drawer::writeVector(vector<T> vec, bool index=true)
{
    for (unsigned int i = 0; i < vec.size(); i++) {
        if (index) {
            cout << i << "\t";
        }
        cout << vec[i] << "\n";
    }
}

我試圖不要重復代碼,同時仍然擔心性能。 在函數中,我正在對for -loop的每一輪進行if (index)檢查,即使結果總是相同的。 這反對“擔心表現”。

我可以通過將支票放在我的for -loop之外來輕松避免這種情況。 但是,我會得到大量重復的代碼:

void Drawer::writeVector(...)
{
    if (index) {
        for (...) {
            cout << i << "\t" << vec[i] << "\n";
        }
    }
    else {
        for (...) {
            cout << vec[i] << "\n";
        }
    }
}

所以這些對我來說都是“壞”的解決方案。 我一直在想的是兩個私有函數,其中一個超出索引,然后調用另一個。 另一個只是超出了價值。 但是,我無法弄清楚如何在我的程序中使用它,我仍然需要if檢查以查看要調用哪一個...

根據問題,多態似乎是一個正確的解決方案。 但我看不出我該怎么用呢。 解決這類問題的首選方法是什么?

這不是一個真正的程序, 我只是想學習如何解決這類問題。

作為仿函數傳遞循環體。 它在編譯時被內聯,沒有性能損失。

傳遞變化的想法在C ++標准庫中無處不在。 它被稱為戰略模式。

如果您被允許使用C ++ 11,您可以執行以下操作:

#include <iostream>
#include <set>
#include <vector>

template <typename Container, typename Functor, typename Index = std::size_t>
void for_each_indexed(const Container& c, Functor f, Index index = 0) {

    for (const auto& e : c)
        f(index++, e);
}

int main() {

    using namespace std;

    set<char> s{'b', 'a', 'c'};

    // indices starting at 1 instead of 0
    for_each_indexed(s, [](size_t i, char e) { cout<<i<<'\t'<<e<<'\n'; }, 1u);

    cout << "-----" << endl;

    vector<int> v{77, 88, 99};

    // without index
    for_each_indexed(v, [](size_t , int e) { cout<<e<<'\n'; });
}

這段代碼並不完美,但你明白了。

在舊的C ++ 98中它看起來像這樣:

#include <iostream>
#include <vector>
using namespace std;

struct with_index {
  void operator()(ostream& out, vector<int>::size_type i, int e) {
    out << i << '\t' << e << '\n';
  }
};

struct without_index {
  void operator()(ostream& out, vector<int>::size_type i, int e) {
    out << e << '\n';
  }
};


template <typename Func>
void writeVector(const vector<int>& v, Func f) {
  for (vector<int>::size_type i=0; i<v.size(); ++i) {
    f(cout, i, v[i]);
  }
}

int main() {

  vector<int> v;
  v.push_back(77);
  v.push_back(88);
  v.push_back(99);

  writeVector(v, with_index());

  cout << "-----" << endl;

  writeVector(v, without_index());

  return 0;
}

同樣,代碼遠非完美,但它給你的想法。

在函數中,我正在對for循環的每一輪進行if(索引)檢查,即使結果總是相同的。 這反對“擔心表現”。

如果確實如此,分支預測器在預測(常量)結果方面沒有問題。 因此,這只會在前幾次迭代中導致錯誤預測的輕微開銷。 在性能方面沒什么好擔心的

在這種情況下,我主張將測試保持在循環內以便清晰。

為了擴展Ali的答案,這是完全正確但仍然復制了一些代碼(循環體的一部分,不幸的是,這在使用策略模式時難以避免)...

在這種特殊情況下,代碼重復並不多,但有一種方法可以將其減少更多, 如果函數體大於幾條指令就會派上用場。

關鍵是使用編譯器執行常量折疊/死代碼消除的能力。 我們可以通過手動將index的運行時值映射到編譯時值來實現這一點(當只有有限數量的情況時容易做到 - 在這種情況下為2)並使用一個非類型模板參數編譯時間:

template<bool index = true>
//                  ^^^^^^ note: the default value is now part of the template version
//                         see below to understand why
void writeVector(const vector<int>& vec) {
    for (size_t i = 0; i < vec.size(); ++i) {
        if (index) { // compile-time constant: this test will always be eliminated
            cout << i << "\t"; // this will only be kept if "index" is true
        }
        cout << vec[i] << "\n";
    }
}

void writeVector(const vector<int>& vec, bool index)
//                                            ^^^^^ note: no more default value, otherwise
//                                            it would clash with the template overload
{
    if (index) // runtime decision
        writeVector<true>(vec);
        //          ^^^^ map it to a compile-time constant
    else
        writeVector<false>(vec);
}

這樣,我們最終編譯的代碼,相當於你的第二個代碼示例(外if /內for ),但沒有復制的代碼自己。 現在我們可以將writeVector的模板版本設置為我們想要的復雜版本, writeVector有一段代碼需要維護。

請注意模板版本(以非類型模板參數的形式采用編譯時常量)和非模板版本(將運行時變量作為函數參數)重載。 這允許您根據需要選擇最相關的版本,在兩種情況下都具有相似且易於記憶的語法:

writeVector<true>(vec);   // you already know at compile-time which version you want
                          // no need to go through the non-template runtime dispatching

writeVector(vec, index);  // you don't know at compile-time what "index" will be
                          // so you have to use the non-template runtime dispatching

writeVector(vec);         // you can even use your previous syntax using a default argument
                          // it will call the template overload directly

在大多數情況下,您的代碼已經具有良好的性能和可讀性。 一個好的編譯器能夠檢測循環不變量並進行適當的優化。 請考慮以下與您的代碼非常接近的示例:

#include <cstdio>
#include <iterator>

void write_vector(int* begin, int* end, bool print_index = false) {
    unsigned index = 0;
    for(int* it = begin; it != end; ++it) {
        if (print_index) {
            std::printf("%d: %d\n", index, *it);
        } else {
            std::printf("%d\n", *it);
        }
        ++index;
    }
}

int my_vector[] = {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
};


int main(int argc, char** argv) {
    write_vector(std::begin(my_vector), std::end(my_vector));
}

我使用以下命令行來編譯它:

g++ --version
g++ (GCC) 4.9.1
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ -O3 -std=c++11 main.cpp

然后,讓我們轉儲匯編:

objdump -d a.out | c++filt > main.s

write_vector的結果匯編是:

00000000004005c0 <write_vector(int*, int*, bool)>:
  4005c0:   48 39 f7                cmp    %rsi,%rdi
  4005c3:   41 54                   push   %r12
  4005c5:   49 89 f4                mov    %rsi,%r12
  4005c8:   55                      push   %rbp
  4005c9:   53                      push   %rbx
  4005ca:   48 89 fb                mov    %rdi,%rbx
  4005cd:   74 25                   je     4005f4 <write_vector(int*, int*, bool)+0x34>
  4005cf:   84 d2                   test   %dl,%dl
  4005d1:   74 2d                   je     400600 <write_vector(int*, int*, bool)+0x40>
  4005d3:   31 ed                   xor    %ebp,%ebp
  4005d5:   0f 1f 00                nopl   (%rax)
  4005d8:   8b 13                   mov    (%rbx),%edx
  4005da:   89 ee                   mov    %ebp,%esi
  4005dc:   31 c0                   xor    %eax,%eax
  4005de:   bf a4 06 40 00          mov    $0x4006a4,%edi
  4005e3:   48 83 c3 04             add    $0x4,%rbx
  4005e7:   83 c5 01                add    $0x1,%ebp
  4005ea:   e8 81 fe ff ff          callq  400470 <printf@plt>
  4005ef:   49 39 dc                cmp    %rbx,%r12
  4005f2:   75 e4                   jne    4005d8 <write_vector(int*, int*, bool)+0x18>
  4005f4:   5b                      pop    %rbx
  4005f5:   5d                      pop    %rbp
  4005f6:   41 5c                   pop    %r12
  4005f8:   c3                      retq   
  4005f9:   0f 1f 80 00 00 00 00    nopl   0x0(%rax)
  400600:   8b 33                   mov    (%rbx),%esi
  400602:   31 c0                   xor    %eax,%eax
  400604:   bf a8 06 40 00          mov    $0x4006a8,%edi
  400609:   48 83 c3 04             add    $0x4,%rbx
  40060d:   e8 5e fe ff ff          callq  400470 <printf@plt>
  400612:   49 39 dc                cmp    %rbx,%r12
  400615:   75 e9                   jne    400600 <write_vector(int*, int*, bool)+0x40>
  400617:   eb db                   jmp    4005f4 <write_vector(int*, int*, bool)+0x34>
  400619:   0f 1f 80 00 00 00 00    nopl   0x0(%rax)

我們可以看到,在函數的乞求時,我們檢查值並跳轉到兩個可能的循環之一:

  4005cf:   84 d2                   test   %dl,%dl
  4005d1:   74 2d                   je     400600 <write_vector(int*, int*, bool)+0x40>

當然,這僅在編譯器能夠檢測到條件是實際不變的情況下才有效。 通常,它完全適用於標志和簡單的內聯函數。 但如果條件“復雜”,請考慮使用其他答案的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM