I'm searching for an efficient way to create random integer vectors in C++. I made some comparison with an equivalent matlab code and the difference in terms of speed was vast; for large vectors matlab needs only 3% the time of C++. Is there a way to speed up the process?
//********C++********//
int m = 200000;
float density = 1.0e-5;
int nnz = density *m*m;
uint32_t seed;
std::mt19937 eng;
std::srand(time(nullptr));
seed = std::rand();
eng.seed(seed);
std::uniform_int_distribution<int> dist{ 0, m };
auto gen = [&dist, &eng]() {
return dist(eng);
};
//elapsed time 256.511 msecs.
std::vector<int> v1(nnz), v2(nnz);
std::generate(std::begin(v1), std::end(v1), gen);
std::generate(std::begin(v2), std::end(v2), gen);
//*******************//
%********MATLAB********%
m=200000;
density = 1.e-5;
nnz = density*m*m;
tic;
% Elapsed time is 9.842 milliseconds.
v1=randi([0 m],nnz,1);
v2=randi([0 m],nnz,1);
toc;
%***********************%
Aucun commentaire:
Enregistrer un commentaire