7

Here is a simple class for iterating over a multidimensional numeric range:

#include <array>
#include <limits>

template <int N>
class NumericRange
{
public:
  //  typedef std::vector<double>::const_iterator const_iterator;
  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());
    _next_index_to_advance = 0;
  }

  const std::array<double, N> & get_state() 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+1);
      }
    }
  }

private:
  std::array<double, N> _lower, _upper, _delta, _state;
  int _next_index_to_advance;
};

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()) {
    const std::array<double, 7> & st = nr.get_state();
    ++c;
  }
  std::cout << "took " << c << " steps" << std::endl;

  return 0;
}

When I replace the advance function with a non-recursive variant, the runtime increases:

void advance(int index_to_advance = 0) {
  bool carry;
  do {
    carry = false;
    _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;
    carry = true;
    //    advance(index_to_advance);
      }
    }
  } while (carry);
}

Runtimes were taken using unix user time via the command time. The code was compiled using gcc-4.7 with options -std=c++11 -O3 (but I think it should work with c++0x on gcc-4.6). The recursive version took 13s and the iterative version took 30s. Both require the same number of advance calls to terminate (and if you print the nr.get_state() array inside the for(ns.start()...) loop, both do the same thing).

This is a fun riddle! Help me figure out why recursive would be more efficient / more optimizable.

4

2 回答 2

13

递归版本是尾递归的一个例子,这意味着编译器可以将递归转换为迭代。现在,一旦执行了转换,递归函数将如下所示:

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

        // carry
        ++index_to_advance;
        _state[ index_to_advance ] += _delta[ index_to_advance ];
    }
  }

如您所见,您的版本包含一个额外的测试和条件变量。循环,如果你仔细观察,相当于

for( ; index_to_advance < N-1 && !in_range(index_to_advance);++index_to_advance)

(最后删除++index_to_advance),优化器可能有更好的机会展开它。

话虽如此,我认为这并不能解释巨大的时间差异,尽管它确实解释了为什么递归版本并不比迭代版本慢很多。检查生成的程序集以查看编译器实际做了什么。

于 2012-06-03T01:10:26.017 回答
3

只是为大卫罗德里格斯所说的添加更多细节:

通过尾递归优化,函数变为:

 void advance(int index_to_advance = 0) {
  top:
  _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;
      goto top;
    }
  }
}

这确实与我系统上的递归版本具有相同的性能(g++ 4.6.3 -std=c++0x)

于 2012-06-03T01:22:44.387 回答