Vin*_*ara 8 random performance boost c++11
我想用C++生成伪随机数,这两个可能的选项是C++ 11和Boost对应的特性.它们的使用方式基本相同,但在我的测试中,原生的大约慢了4倍.
这是由于库中的设计选择,还是我错过了某种方法在某处禁用调试代码?
更新:代码在这里,https://github.com/vbeffara/Simulations/blob/master/tests/test_prng.cpp,如下所示:
cerr << "boost::bernoulli_distribution ... \ttime = ";
s=0; t=time();
boost::bernoulli_distribution<> dist(.5);
boost::mt19937 boostengine;
for (int i=0; i<n; ++i) s += dist(boostengine);
cerr << time()-t << ", \tsum = " << s << endl;
cerr << "C++11 style ... \ttime = ";
s=0; t=time();
std::bernoulli_distribution dist2(.5);
std::mt19937_64 engine;
for (int i=0; i<n; ++i) s += dist2(engine);
cerr << time()-t << ", \tsum = " << s << endl;
Run Code Online (Sandbox Code Playgroud)
(使用std::mt19937而不是std::mt19937_64在我的系统上使它更慢.)
这太可怕了.
我们来看一下:
升压:: bernoulli_distribution <>
if(_p == RealType(0))
return false;
else
return RealType(eng()-(eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
Run Code Online (Sandbox Code Playgroud)
的std :: bernoulli_distribution
__detail::_Adaptor<_UniformRandomNumberGenerator, double> __aurng(__urng);
if ((__aurng() - __aurng.min()) < __p.p() * (__aurng.max() - __aurng.min()))
return true;
return false;
Run Code Online (Sandbox Code Playgroud)
两个版本都调用引擎并检查输出是否位于与给定概率成比例的值范围的一部分.
最大的区别是,gcc版本调用了辅助类的功能_Adaptor.
这个类min和max函数分别返回0并调用给定的URNG来获取和之间的值.1operator()std::generate_canonical01
std::generate_canonical 是一个带有循环的20行函数 - 在这种情况下它永远不会迭代多次,但它增加了复杂性.
除此之外,boost使用param_type分布的构造函数中唯一的,但然后保存_p为double成员,而gcc有一个param_type成员,并且必须"获取"它的值.
这一切都汇集在一起,编译器在优化方面失败.锵扼流圈更就可以了.
如果你捶够硬,你甚至可以得到std::mt19937和boost::mt19937EN标准杆GCC.
测试libc ++也不错,也许我稍后会添加它.
测试版本:
根据请求升级1.55.0,gcc 4.8.2 行号的libstdc ++头文件^^
| 归档时间: |
|
| 查看次数: |
2328 次 |
| 最近记录: |