github.com/songzhibin97/gkit@v1.2.13/sys/fastrand/fastrand_test.go (about) 1 package fastrand 2 3 import ( 4 "math/rand" 5 "testing" 6 ) 7 8 func TestAll(t *testing.T) { 9 _ = Uint32() 10 11 p := make([]byte, 1000) 12 n, err := Read(p) 13 if n != len(p) || err != nil || (p[0] == 0 && p[1] == 0 && p[2] == 0) { 14 t.Fatal() 15 } 16 17 a := Perm(100) 18 for i := range a { 19 var find bool 20 for _, v := range a { 21 if v == i { 22 find = true 23 } 24 } 25 if !find { 26 t.Fatal() 27 } 28 } 29 30 Shuffle(len(a), func(i, j int) { 31 a[i], a[j] = a[j], a[i] 32 }) 33 for i := range a { 34 var find bool 35 for _, v := range a { 36 if v == i { 37 find = true 38 } 39 } 40 if !find { 41 t.Fatal() 42 } 43 } 44 } 45 46 func BenchmarkSingleCore(b *testing.B) { 47 b.Run("math/rand-Uint32()", func(b *testing.B) { 48 for i := 0; i < b.N; i++ { 49 _ = rand.Uint32() 50 } 51 }) 52 b.Run("fast-rand-Uint32()", func(b *testing.B) { 53 for i := 0; i < b.N; i++ { 54 _ = Uint32() 55 } 56 }) 57 58 b.Run("math/rand-Uint64()", func(b *testing.B) { 59 for i := 0; i < b.N; i++ { 60 _ = rand.Uint64() 61 } 62 }) 63 b.Run("fast-rand-Uint64()", func(b *testing.B) { 64 for i := 0; i < b.N; i++ { 65 _ = Uint64() 66 } 67 }) 68 69 b.Run("math/rand-Read1000", func(b *testing.B) { 70 p := make([]byte, 1000) 71 b.ResetTimer() 72 for i := 0; i < b.N; i++ { 73 rand.Read(p) 74 } 75 }) 76 b.Run("fast-rand-Read1000", func(b *testing.B) { 77 p := make([]byte, 1000) 78 b.ResetTimer() 79 for i := 0; i < b.N; i++ { 80 Read(p) 81 } 82 }) 83 84 } 85 86 func BenchmarkMultipleCore(b *testing.B) { 87 b.Run("math/rand-Uint32()", func(b *testing.B) { 88 b.RunParallel(func(pb *testing.PB) { 89 for pb.Next() { 90 _ = rand.Uint32() 91 } 92 }) 93 }) 94 b.Run("fast-rand-Uint32()", func(b *testing.B) { 95 b.RunParallel(func(pb *testing.PB) { 96 for pb.Next() { 97 _ = Uint32() 98 } 99 }) 100 }) 101 102 b.Run("math/rand-Uint64()", func(b *testing.B) { 103 b.RunParallel(func(pb *testing.PB) { 104 for pb.Next() { 105 _ = rand.Uint64() 106 } 107 }) 108 }) 109 b.Run("fast-rand-Uint64()", func(b *testing.B) { 110 b.RunParallel(func(pb *testing.PB) { 111 for pb.Next() { 112 _ = Uint64() 113 } 114 }) 115 }) 116 117 b.Run("math/rand-Read1000", func(b *testing.B) { 118 p := make([]byte, 1000) 119 b.ResetTimer() 120 b.RunParallel(func(pb *testing.PB) { 121 for pb.Next() { 122 rand.Read(p) 123 } 124 }) 125 }) 126 b.Run("fast-rand-Read1000", func(b *testing.B) { 127 p := make([]byte, 1000) 128 b.ResetTimer() 129 b.RunParallel(func(pb *testing.PB) { 130 for pb.Next() { 131 Read(p) 132 } 133 }) 134 }) 135 }