循环范围更快(C ++ 11)

时间:2012-06-13 05:13:48

标签: c++ optimization iterator c++11

我有一些代码可以迭代(多变量)数值范围:

#include <array>
#include <limits>
#include <iostream>
#include <iterator>

template <int N>
class NumericRange : public std::iterator<double, std::input_iterator_tag>
{
public:
  NumericRange() {
    _lower.fill(std::numeric_limits<double>::quiet_NaN());
    _upper.fill(std::numeric_limits<double>::quiet_NaN());
    _delta.fill(std::numeric_limits<double>::quiet_NaN());
  }
  NumericRange(const std::array<double, N> & lower, const std::array<double, N> & upper, const std::array<double, N> & delta):
    _lower(lower), _upper(upper), _delta(delta) {
    _state.fill(std::numeric_limits<double>::quiet_NaN());
  }

  const std::array<double, N> & get_state() const {
    return _state;
  }

  NumericRange<N> begin() const {
    NumericRange<N> result = *this;
    result.start();
    return result;
  }

  NumericRange<N> end() const {
    NumericRange<N> result = *this;
    result._state = _upper;
    return result;
  }

  bool operator !=(const NumericRange<N> & rhs) const {
    return in_range();
    //    return ! (*this == rhs);
  }

  bool operator ==(const NumericRange<N> & rhs) const {
    return _state == rhs._state && _lower == rhs._lower && _upper == rhs._upper && _delta == rhs._delta;
  }

  const NumericRange<N> & operator ++() {
    advance();
    if ( ! in_range() )
      _state = _upper;
    return *this;
  }

  const std::array<double, N> & operator *() const {
    return _state;
  }

  void start() {
    _state = _lower;
  }

  bool in_range(int index_to_advance = N-1) const {
    return ( _state[ index_to_advance ] - _upper[ index_to_advance ] ) < _delta[ index_to_advance ];
  }

  void advance(int index_to_advance = 0) {
    _state[ index_to_advance ] += _delta[ index_to_advance ];
    if ( ! in_range(index_to_advance) ) {
      if (index_to_advance < N-1) {
    // restart index_to_advance
    _state[index_to_advance] = _lower[index_to_advance];

    // carry
    ++index_to_advance;
    advance(index_to_advance);
      }
    }
  }
private:
  std::array<double, N> _lower, _upper, _delta, _state;
};

int main() {
   std::array<double, 7> lower{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
   std::array<double, 7> upper{{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}};
   std::array<double, 7> delta{{0.03, 0.06, 0.03, 0.06, 0.03, 0.06, 0.03}};

  NumericRange<7> nr(lower, upper, delta);
  int c = 0;    
  for (nr.start(); nr.in_range(); nr.advance()) {
    ++c;
  }
  std::cout << "took " << c << " steps" << std::endl;    
  return 0;
}

使用g++ -std=c++11 -O3(或-std=c++0x使用gcc&lt; 4.7)进行编译,在我的计算机上运行大约13.8秒。

如果我更改main函数以使用基于范围的for循环:

  for (const std::array<double, 7> & arr : nr) {
    ++c;
  }

运行时增加到29.8秒。巧合的是,这个~30秒的运行时与使用std::vector<double>而不是std::array<double, N>的原始运行时几乎相同,这使我相信编译器无法展开生成的代码通过基于范围的for循环。

有没有办法获得原始速度并仍然使用基于范围的for循环?


我尝试了什么:

通过更改NumericRange中的两个成员函数,我可以使用基于范围的for循环获得所需的速度:

bool operator !=(const NumericRange<N> & rhs) const {
  return in_range();
  //    return ! (*this == rhs);
}

const NumericRange<N> & operator ++() {
  advance();
  //    if ( ! in_range() )
  //      _state = _upper;
  return *this;
}

但是,此代码设计不合理,因为!= operator无法正常工作通常对于数值运算,我使用<来终止循环而不是==。我想找到第一个超出范围的值,但是由于数值误差,分析可能无法给出确切的答案。

如何强制!= operator的行为与<类似,而不会误导会看到我的代码的其他人?我只是制作begin()end()函数是私有的,但它们需要公开用于基于范围的for循环。

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:13)

就我而言,问题在于你没有适当地使用范围构造。


让我们退后一步:

void foo(std::vector<int> const& v) {
    for (int i: v) {
    }
}

注意range-for如何迭代向量以提取整数


由于某些原因,您选择不实现迭代器以从begin桥接到end,而是重复使用您正在迭代的副本,即使它只是稍微变化,并且你正在做一个的额外工作(在复制和检查中)......

注意:std::iterator<double, ...>表示operator*应该返回double&

如果你想使用新的习语,你必须符合它的期望。

期望您使用迭代器进行迭代,而不是一遍又一遍地复制原始对象(稍加修改)。这是C ++习语。

这意味着你需要将对象切成两半:在迭代过程中迭代所有迭代的东西以及迭代器中的修改内容。

从我所看到的:

  • _lower_upper_delta已修复
  • _state是迭代变量

因此,您将拥有:

template <typename> class NumericRangeIterator

template <unsigned N> // makes no sense having a negative here
class NumericRange {
public:
    template <typename> friend class NumericRangeIterator;

    typedef NumericRangeIterator<NumericRange> iterator;
    typedef NumericRangeIterator<NumericRange const> const_iterator;

    static unsigned const Size = N;

    // ... constructors

    iterator begin(); // to be defined after NumericRangeIterator
    iterator end();

    const_iterator begin() const;
    const_iterator end() const;

private:
    std::array<double, N> _lower, _upper, _delta;
}; // class NumericRange

template <typename T>
class NumericRangeIterator: public
    std::iterator< std::array<double, T::Size>,
                   std::forward_iterator_tag >
{
public:
    template <unsigned> friend class NumericRange;

    NumericRangeIterator(): _range(0), _state() {}

    NumericRangeIterator& operator++() {
        this->advance();
        return *this;
    }

    NumericRangeIterator operator++(int) {
        NumericRangeIterator tmp(*this);
        ++*this;
        return tmp;
    }

    std::array<double, T::Size> const& operator*() const {
        return _state;
    }

    std::array<double, T::Size> const* operator->() const {
        return _state;
    }

    bool operator==(NumericRangeIterator const& other) const {
        return _state != other._state;
    }

    bool operator!=(NumericRangeIterator const& other) const {
        return !(*this == other);
    }


private:
    NumericRangeIterator(T& t, std::array<double, T::Size> s):
        _range(&t), _state(s) {}

    void advance(unsigned index = T::Size - 1);  // as you did
    void in_range(unsigned index = T::Size - 1); // as you did

    T* _range;
    std::array<double, T::Size> _state;
}; // class NumericRangeIterator


template <unsigned N>
auto NumericRange<N>::begin() -> typename NumericRange<N>::iterator {
    return iterator(*this, _lower);
}

template <unsigned N>
auto NumericRange<N>::end() -> typename NumericRange<N>::iterator {
    return iterator(*this, _upper);
}

通过所有这些设置,您可以写:

for (auto const& state: nr) {
}

auto推断为std::array<double, nr::Size>

注意:不确定iterator是否有用,可能只有const_iterator,因为它在某种程度上是错误的迭代;你无法通过迭代器进入范围对象来修改它。

编辑: operator==太慢,如何让它变得更好?

我建议作弊。

1 /修改迭代器的构造函数

NumericRangeIterator(): _range(0), _state() {}               // sentinel value
NumericRangeIterator(T& t): _range(&t), _state(t._lower) {}

2 /调整迭代以在末尾创建新的“sentinel”值

void advance() {
    // ...

    if (not this->in_range()) {        // getting out of the iteration ?
       *this = NumericRangeIterator(); // then use the sentinel value
    }
}

3 /相应地更改beginend定义

template <unsigned N>
auto NumericRange<N>::begin() -> typename NumericRange<N>::iterator {
    return iterator(*this);
}

template <unsigned N>
auto NumericRange<N>::end() -> typename NumericRange<N>::iterator {
    return iterator();
}

4 /使用sentinel

使==更加平​​等
bool operator==(NumericRangeIterator const& other) const {
    return _range == other._range and _state == other._state;
}

现在,==一直在迭代,因为_range中的一个为空而另一个不为。只有在最后一次调用时才会比较两个_state属性。