超快速梅森旋转SFMT(SIMD-oriented Fast Mersenne Twister)一览

众所周知mt19937能够快速产生高质量伪随机数,可以随便跑个1e8次。
但是如果需要大量随机数mt19937的速度就跟不上了。
最近无意间翻到了一个gnu私货sfmt19937,其定义在ext/random中,速度极快。
以下是测试代码及其耗时。

#include <cstdio>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <ext/random>
#define _A
#ifdef _A
__gnu_cxx::sfmt19937 rnd(114);
#endif
#ifdef _B
std::mt19937 rnd(114);
#endif
#ifdef _C
inline int rnd(void)
{
	static unsigned x=114;
	return x=(x*998244353+19198001);
}
#endif
unsigned int tmp;
int main()
{
	for(int i=1; i<=1e9; ++i)
	{
		tmp+=rnd();
	}
	printf("%u
", tmp);
	return 0;
}

经测试,mt19937耗时2.8s,线性同余耗时1.2s,而sfmt19937仅耗时0.8s

原文地址:https://www.cnblogs.com/ynycoding/p/15499823.html