github.com/CAFxX/fastrand@v0.1.0/fastrand_bench_test.go (about) 1 package fastrand 2 3 import ( 4 "math/rand" 5 "sync" 6 "testing" 7 8 valyala_fastrand "github.com/valyala/fastrand" 9 ) 10 11 func BenchmarkSplitMix64(b *testing.B) { 12 b.RunParallel(func(pb *testing.PB) { 13 var r SplitMix64 14 r.Seed(Seed()) 15 for pb.Next() { 16 use64(r.Uint64()) 17 } 18 }) 19 } 20 21 func BenchmarkAtomicSplitMix64(b *testing.B) { 22 var r AtomicSplitMix64 23 r.Seed(Seed()) 24 b.RunParallel(func(pb *testing.PB) { 25 for pb.Next() { 26 use64(r.Uint64()) 27 } 28 }) 29 } 30 31 func BenchmarkShardedSplitMix64(b *testing.B) { 32 r := NewShardedSplitMix64() 33 b.RunParallel(func(pb *testing.PB) { 34 for pb.Next() { 35 use64(r.Uint64()) 36 } 37 }) 38 } 39 40 func BenchmarkPCG(b *testing.B) { 41 b.RunParallel(func(pb *testing.PB) { 42 var r PCG 43 r.Seed(Seed()) 44 for pb.Next() { 45 use32(r.Uint32()) 46 } 47 }) 48 } 49 50 func BenchmarkAtomicPCG(b *testing.B) { 51 var r AtomicPCG 52 r.Seed(Seed()) 53 b.RunParallel(func(pb *testing.PB) { 54 for pb.Next() { 55 use32(r.Uint32()) 56 } 57 }) 58 } 59 60 func BenchmarkShardedPCG(b *testing.B) { 61 r := NewShardedPCG() 62 b.RunParallel(func(pb *testing.PB) { 63 for pb.Next() { 64 use32(r.Uint32()) 65 } 66 }) 67 } 68 69 func BenchmarkXoshiro256StarStar(b *testing.B) { 70 b.RunParallel(func(pb *testing.PB) { 71 r := &Xoshiro256StarStar{} 72 r.safeSeed() 73 for pb.Next() { 74 use64(r.Uint64()) 75 } 76 }) 77 } 78 79 func BenchmarkShardedXoshiro256StarStar(b *testing.B) { 80 r := NewShardedXoshiro256StarStar() 81 b.RunParallel(func(pb *testing.PB) { 82 for pb.Next() { 83 use64(r.Uint64()) 84 } 85 }) 86 } 87 88 func BenchmarkMathRand(b *testing.B) { 89 b.RunParallel(func(pb *testing.PB) { 90 r := rand.New(rand.NewSource(0)) 91 for pb.Next() { 92 use64(r.Uint64()) 93 } 94 }) 95 } 96 97 func BenchmarkMathRandMutex(b *testing.B) { 98 var m sync.Mutex 99 r := rand.New(rand.NewSource(0)) 100 b.RunParallel(func(pb *testing.PB) { 101 for pb.Next() { 102 m.Lock() 103 use64(r.Uint64()) 104 m.Unlock() 105 } 106 }) 107 } 108 109 func BenchmarkValyalaFastrand(b *testing.B) { 110 b.RunParallel(func(pb *testing.PB) { 111 for pb.Next() { 112 use32(valyala_fastrand.Uint32()) 113 } 114 }) 115 } 116 117 //go:noinline 118 func use32(uint32) {} 119 120 //go:noinline 121 func use64(uint64) {}