C ++:绘制随机布尔值的性能 [英] C++: Performance for drawing random boolean values
本文介绍了C ++:绘制随机布尔值的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用
std::mt19937 rng;
std::uniform_int_distribution<int> dis(0, 1);
在极端情况下,绘制这些数字可能会占用我进程的CPU时间的40%.有什么方法可以使速度更快?
In extreme cases, drawing these numbers can take up to 40% of the CPU time of my process. Is there any way to make this faster?
推荐答案
我会摆脱 uniform_int_distribution
.
一次调用 std :: mt19937 :: operator()
(即执行 rng()
)将为您提供32个随机位,您可以一一使用
One invocation of std::mt19937::operator()
(i.e. doing rng()
) will give you 32 random bits, which you can use one by one.
基准测试显示该方法的速度大约提高了23倍.
A benchmark shows that this method is about 23 times faster.
这篇关于C ++:绘制随机布尔值的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文