我需要采样一个巨大的随机矩阵,其大小为499 x 15500
,即7734500
个元素。因此,我希望抽样程序尽可能高效。目前,在cpp文件中我这样做:
std::random_device rd; // seed generator
std::mt19937_64 generator{rd()}; // generator initialized with seed from rd
std::uniform_int_distribution<> initialize(unsigned long long int modulus)
{
std::uniform_int_distribution<> unifDist{0, (int)(modulus-1)};
return unifDist;
}
Matrix<unsigned int> uniformRandomMatrix
(unsigned int rows, unsigned int columns, unsigned long long int modulus)
{
std::uniform_int_distribution<> dist = initialize(modulus);
// Declare and allocate the matrix
Matrix<unsigned int> matrix(rows, columns);
// this constructor just does a resize on a std::vector
// Fill the matrix with random elements
for(unsigned int i = 0; i < rows; ++i)
{
for(unsigned int j = 0; j < columns; ++j)
{
matrix.setElementAt(i, j, dist(generator));
// setElementAt just does matrix[somePosition] = newElement
}
}
return matrix;
}
请注意,Matrix
实现为1D std::vector
以提高效率。
我能做得比这更好吗?现在,对这个庞大的矩阵进行采样大约需要0.16秒。
std::vector::data
新方法是这样的:我在Matrix
类中添加了一个方法
inline std::vector<T> exposeVector()
{
return matrix; // 'matrix' is the name of the private std::vector
}
然后采样制服Matrix
Matrix<unsigned int> uniformRandomMatrix
(unsigned int rows, unsigned int columns, unsigned long long int modulus)
{
std::uniform_int_distribution<> dist = initialize(modulus);
// Declare and allocate the matrix
Matrix<unsigned int> matrix(rows, columns);
std::vector<unsigned int> v = matrix.exposeVector();
unsigned int* p = v.data();
for(unsigned int i = 0 ; i < rows*columns ; ++i)
{
*p = dist(generator);
++p;
}
return matrix;
}
一切似乎都运转良好,但我在表现方面没有获得任何好处。
答案 0 :(得分:0)
在我看来,这是一个非常广泛的问题,但我会尝试给你一些指示:
std::thred
setElementAt
是虚拟的,那么由于vtable解析它非常昂贵。摆脱它(见下一行)for(int i = 0; i < rows*cols; i++); data[i] = random
或尝试std::generate来尝试最小化函数/方法调用