使用梅森捻线机随机双生成的性能问题
Performance issue with random double generation using Mersenne twister
我有以下代码:
//#include all necessary things
class RandomGenerator {
public:
double GetRandomDbl() {
random_device rd;
mt19937 eng(rd());
std::uniform_real_distribution<double> dDistribution(0,1);
return dDistribution(eng);
}
};
然后我有:
int _tmain(int argc, _TCHAR* argv[])
{
RandomGenerator Rand; //on the heap for now
for (int i = 0; i < 1000000; i++) {
double pF = Rand.GetRandomDbl();
}
}
仅此代码就需要惊人的 25-28 秒才能在 4GB RAM 上执行。我记得每次使用 Mersenne twister 时都会读到一些关于实例化新对象的内容,但如果这是问题所在,我应该如何改进呢?当然,这可以做得更快。
不需要
在 GetRandomDbl
中创建伪随机数生成器对象。试试这个:
//#include all necessary things
class RandomGenerator {
public:
RandomGenerator() : eng(rd()), dDistribution(0, 1) {}
double GetRandomDbl() {
return dDistribution(eng);
}
private:
random_device rd;
mt19937 eng;
std::uniform_real_distribution<double> dDistribution;
};
相关文章:
- 在类中使用随机生成器时出现性能问题
- Qt OpenGL 渲染到纹理性能问题
- 剪辑性能问题
- OpenCV - 基本操作 - 性能问题 [模式:发布]
- 使用 #define 进行跟踪日志记录以避免性能问题
- 在 Qt C++ 中在自定义项委托上绘制文本时的性能问题
- std::函数有性能问题,如何避免?
- 使用 const double* const 作为模板参数 - 代码性能问题
- 在C 中读取大型CSV文件性能问题
- MPI 二进制文件 I/O 基本功能和性能问题
- 使用STD :: MAP在数据及其性能问题中查找重复项.我可以预先分配吗?
- C 功能性能问题
- 在C 性能问题中使用Getter返回地图
- 初始化每个班级成员时的性能问题
- 事件(开始,结束),1天的最大事件.C++ 中的性能问题
- 在X64模式下从C /CLI调用MASM PROC会产生意外的性能问题
- OpenGL:MESA3D屏幕上的软件渲染性能问题
- 是包含容器性能问题的STL关联容器
- 任何性能问题都在qt框架中使用了stackedwidget的最大页面数
- SHGetFileInfo性能问题