我目前正在为C++编写一个linalg库,用于教育目的和个人使用。作为它的一部分,我使用自定义行和列迭代器实现了一个自定义矩阵类。虽然提供了使用std :: algorithm和std :: numeric函数的非常好的功能,但我在索引和iterator/std :: inner_product方法之间执行了矩阵乘法的速度比较。结果显著不同:C++速度比较迭代器与索引
// used later on for the custom iterator
template<class U>
struct EveryNth {
bool operator()(const U&) { return m_count++ % N == 0; }
EveryNth(std::size_t i) : m_count(0), N(i) {}
EveryNth(const EveryNth& element) : m_count(0), N(element.N) {}
private:
int m_count;
std::size_t N;
};
template<class T,
std::size_t rowsize,
std::size_t colsize>
class Matrix
{
private:
// Data is stored in a MVector, a modified std::vector
MVector<T> matrix;
std::size_t row_dim;
std::size_t column_dim;
public:
// other constructors, this one is for matrix in the computation
explicit Matrix(MVector<T>&& s): matrix(s),
row_dim(rowsize),
column_dim(colsize){
}
// other code...
typedef boost::filter_iterator<EveryNth<T>,
typename std::vector<T>::iterator> FilterIter;
// returns an iterator that skips elements in a range
// if "to" is to be specified, then from has to be set to a value
// @ param "j" - j'th column to be requested
// @ param "from" - starts at the from'th element
// @ param "to" - goes from the from'th element to the "to'th" element
FilterIter begin_col(std::size_t j,
std::size_t from = 0,
std::size_t to = rowsize){
return boost::make_filter_iterator<EveryNth<T> >(
EveryNth<T>(cols()),
matrix.Begin() + index(from, j),
matrix.Begin() + index(to, j)
);
}
// specifies then end of the iterator
// so that the iterator can not "jump" past the last element into undefines behaviour
FilterIter end_col(std::size_t j,
std::size_t to = rowsize){
return boost::make_filter_iterator<EveryNth<T> >(
EveryNth<T>(cols()),
matrix.Begin() + index(to, j),
matrix.Begin() + index(to, j)
);
}
FilterIter begin_row(std::size_t i,
std::size_t from = 0,
std::size_t to = colsize){
return boost::make_filter_iterator<EveryNth<T> >(
EveryNth<T>(1),
matrix.Begin() + index(i, from),
matrix.Begin() + index(i, to)
);
}
FilterIter end_row(std::size_t i,
std::size_t to = colsize){
return boost::make_filter_iterator<EveryNth<T> >(
EveryNth<T>(1),
matrix.Begin() + index(i, to),
matrix.Begin() + index(i, to)
);
}
// other code...
// allows to access an element of the matrix by index expressed
// in terms of rows and columns
// @ param "r" - r'th row of the matrix
// @ param "c" - c'th column of the matrix
std::size_t index(std::size_t r, std::size_t c) const {
return r*cols()+c;
}
// brackets operator
// return an elements stored in the matrix
// @ param "r" - r'th row in the matrix
// @ param "c" - c'th column in the matrix
T& operator()(std::size_t r, std::size_t c) {
assert(r < rows() && c < matrix.size()/rows());
return matrix[index(r,c)];
}
const T& operator()(std::size_t r, std::size_t c) const {
assert(r < rows() && c < matrix.size()/rows());
return matrix[index(r,c)];
}
// other code...
// end of class
};
现在在运行的主要功能如下:
int main(int argc, char *argv[]){
Matrix<int, 100, 100> a = Matrix<int, 100, 100>(range<int>(10000));
std::clock_t begin = clock();
double b = 0;
for(std::size_t i = 0; i < a.rows(); i++){
for (std::size_t j = 0; j < a.cols(); j++) {
std::inner_product(a.begin_row(i), a.end_row(i),
a.begin_column(j),0);
}
}
// double b = 0;
// for(std::size_t i = 0; i < a.rows(); i++){
// for (std::size_t j = 0; j < a.cols(); j++) {
// for (std::size_t k = 0; k < a.rows(); k++) {
// b += a(i,k)*a(k,j);
// }
// }
// }
std::clock_t end = clock();
double elapsed_secs = double(end - begin)/CLOCKS_PER_SEC;
std::cout << elapsed_secs << std::endl;
std::cout << "--- End of test ---" << std::endl;
std::cout << std::endl;
return 0;
}
对于性病:: inner_product /迭代器,它需要的方法:
bash-3.2$ ./main
3.78358
--- End of test ---
和索引(// out)方法:
bash-3.2$ ./main
0.106173
--- End of test ---
这比迭代器方法快了近40倍。你看到代码中的任何东西都会减慢迭代器计算的速度吗?我应该提及的是,我尝试了两种方法,并产生了正确的结果。
谢谢你的想法。
它可能属于代码审查......你也应该补充你的编译器,以及所有编译器选项。 – quantdev 2014-10-11 12:17:21
看起来你并没有使用你的迭代器迭代,而是每次都创建新的迭代器。或者我读错了吗? – Galik 2014-10-11 12:21:25
@Galik:更新了帖子。复制时发生错误。 – Vincent 2014-10-11 12:31:55