gitee.com/quant1x/num@v0.3.2/internal/functions/boolean_test.go (about) 1 package functions 2 3 import ( 4 "fmt" 5 "gitee.com/quant1x/num/internal/rand" 6 "gitee.com/quant1x/pkg/testify/require" 7 "slices" 8 "testing" 9 ) 10 11 func TestLogic(t *testing.T) { 12 rand.Seed(2) 13 for i := 0; i < 1000; i++ { 14 size := 1 + (i / 5) 15 { 16 r1 := RandomBool(size, 0.5) 17 r2 := slices.Clone(r1) 18 19 Not_Go(r1) 20 Not_AVX2(r2) 21 require.Equal(t, r1, r2) 22 } 23 { 24 y := RandomBool(size, 0.5) 25 r1 := RandomBool(size, 0.5) 26 r2 := slices.Clone(r1) 27 28 And_Go(r1, y) 29 And_AVX2(r2, y) 30 require.Equal(t, r1, r2) 31 } 32 { 33 y := RandomBool(size, 0.5) 34 r1 := RandomBool(size, 0.5) 35 r2 := slices.Clone(r1) 36 37 Or_Go(r1, y) 38 Or_AVX2(r2, y) 39 require.Equal(t, r1, r2) 40 } 41 { 42 y := RandomBool(size, 0.5) 43 r1 := RandomBool(size, 0.5) 44 r2 := slices.Clone(r1) 45 46 Xor_Go(r1, y) 47 Xor_AVX2(r2, y) 48 require.Equal(t, r1, r2) 49 } 50 } 51 } 52 53 func TestAllAnyNone(t *testing.T) { 54 rand.Seed(2) 55 for i := 0; i < 1000; i++ { 56 size := 1 + (i / 5) 57 { 58 x := RandomBool(size, 0) 59 require.Equal(t, All_Go(x), All_AVX2(x) != 0) 60 61 x = RandomBool(size, 0.5) 62 require.Equal(t, All_Go(x), All_AVX2(x) != 0) 63 64 x = RandomBool(size, 1) 65 require.Equal(t, All_Go(x), All_AVX2(x) != 0) 66 } 67 { 68 x := RandomBool(size, 0) 69 require.Equal(t, Any_Go(x), Any_AVX2(x) != 0) 70 71 x = RandomBool(size, 0.5) 72 require.Equal(t, Any_Go(x), Any_AVX2(x) != 0) 73 74 x = RandomBool(size, 1) 75 require.Equal(t, Any_Go(x), Any_AVX2(x) != 0) 76 } 77 { 78 x := RandomBool(size, 0) 79 require.Equal(t, None_Go(x), None_AVX2(x) != 0) 80 81 x = RandomBool(size, 0.5) 82 require.Equal(t, None_Go(x), None_AVX2(x) != 0) 83 84 x = RandomBool(size, 1) 85 require.Equal(t, None_Go(x), None_AVX2(x) != 0) 86 } 87 } 88 } 89 90 func TestCount(t *testing.T) { 91 rand.Seed(2) 92 for i := 0; i < 1000; i++ { 93 size := 1 + (i / 5) 94 { 95 r1 := RandomBool(size, 0.5) 96 r2 := slices.Clone(r1) 97 98 Count_Go(r1) 99 Count_AVX2(r2) 100 require.Equal(t, r1, r2) 101 } 102 } 103 } 104 105 func BenchmarkLogic(b *testing.B) { 106 for _, size := range sizes[3:] { 107 x := RandomBool(size, 0.5) 108 y := RandomBool(size, 0.5) 109 110 b.Run(fmt.Sprintf("go_not_%d", size), func(b *testing.B) { 111 for i := 0; i < b.N; i++ { 112 b.StopTimer() 113 x := slices.Clone(x) 114 b.StartTimer() 115 Not_Go(x) 116 } 117 }) 118 b.Run(fmt.Sprintf("avx2_not_%d", size), func(b *testing.B) { 119 for i := 0; i < b.N; i++ { 120 b.StopTimer() 121 x := slices.Clone(x) 122 b.StartTimer() 123 Not_AVX2(x) 124 } 125 }) 126 b.Run(fmt.Sprintf("go_and_%d", size), func(b *testing.B) { 127 for i := 0; i < b.N; i++ { 128 b.StopTimer() 129 x := slices.Clone(x) 130 b.StartTimer() 131 And_Go(x, y) 132 } 133 }) 134 b.Run(fmt.Sprintf("avx2_and_%d", size), func(b *testing.B) { 135 for i := 0; i < b.N; i++ { 136 b.StopTimer() 137 x := slices.Clone(x) 138 b.StartTimer() 139 And_AVX2(x, y) 140 } 141 }) 142 } 143 } 144 145 func BenchmarkAllAnyNone(b *testing.B) { 146 for _, size := range sizes { 147 x := RandomBool(size, 1) 148 y := RandomBool(size, 0) 149 x[size/2] = false 150 y[size/2] = true 151 152 b.Run(fmt.Sprintf("go_all_%d", size), func(b *testing.B) { 153 for i := 0; i < b.N; i++ { 154 All_Go(x) 155 } 156 }) 157 b.Run(fmt.Sprintf("avx2_all_%d", size), func(b *testing.B) { 158 for i := 0; i < b.N; i++ { 159 All_AVX2(x) 160 } 161 }) 162 b.Run(fmt.Sprintf("go_any_%d", size), func(b *testing.B) { 163 for i := 0; i < b.N; i++ { 164 Any_Go(y) 165 } 166 }) 167 b.Run(fmt.Sprintf("avx2_any_%d", size), func(b *testing.B) { 168 for i := 0; i < b.N; i++ { 169 Any_AVX2(y) 170 } 171 }) 172 b.Run(fmt.Sprintf("go_none_%d", size), func(b *testing.B) { 173 for i := 0; i < b.N; i++ { 174 None_Go(y) 175 } 176 }) 177 b.Run(fmt.Sprintf("avx2_none_%d", size), func(b *testing.B) { 178 for i := 0; i < b.N; i++ { 179 None_AVX2(y) 180 } 181 }) 182 } 183 } 184 185 func BenchmarkCount(b *testing.B) { 186 for _, size := range sizes { 187 x := RandomBool(size, 0.5) 188 189 b.Run(fmt.Sprintf("go_%d", size), func(b *testing.B) { 190 for i := 0; i < b.N; i++ { 191 Count_Go(x) 192 } 193 }) 194 b.Run(fmt.Sprintf("avx2_%d", size), func(b *testing.B) { 195 for i := 0; i < b.N; i++ { 196 Count_AVX2(x) 197 } 198 }) 199 } 200 }