gitee.com/quant1x/num@v0.3.2/internal/functions/construct_test.go (about) 1 package functions 2 3 import ( 4 "bytes" 5 "fmt" 6 "gitee.com/quant1x/num/internal/rand" 7 "gitee.com/quant1x/pkg/testify/require" 8 "math" 9 "testing" 10 ) 11 12 func TestRepeat(t *testing.T) { 13 rand.Seed(2) 14 for i := 0; i < 1000; i++ { 15 size := 1 + (i / 5) 16 { 17 a := rand.NormFloat64() 18 r1 := make([]float64, size) 19 r2 := make([]float64, size) 20 Repeat_AVX2_F64(r1, a, size) 21 Repeat_Go(r2, a, size) 22 require.InDeltaSlice(t, r1, r2, 0.001) 23 } 24 { 25 a := float32(rand.NormFloat64()) 26 r1 := make([]float32, size) 27 r2 := make([]float32, size) 28 Repeat_AVX2_F32(r1, a, size) 29 Repeat_Go(r2, a, size) 30 require.InDeltaSlice(t, r1, r2, 0.001) 31 } 32 } 33 } 34 35 func TestRange(t *testing.T) { 36 rand.Seed(2) 37 for i := 0; i < 1000; i++ { 38 size := 1 + (i / 5) 39 { 40 a := math.Round(rand.NormFloat64() * 100) 41 r1 := make([]float64, size) 42 r2 := make([]float64, size) 43 Range_AVX2_F64(r1, a, size) 44 Range_Go(r2, a, size) 45 require.InDeltaSlice(t, r1, r2, 0.001) 46 } 47 { 48 a := float32(math.Round(rand.NormFloat64() * 100)) 49 r1 := make([]float32, size) 50 r2 := make([]float32, size) 51 Range_AVX2_F32(r1, a, size) 52 Range_Go(r2, a, size) 53 require.InDeltaSlice(t, r1, r2, 0.001) 54 } 55 } 56 } 57 58 func TestFrom(t *testing.T) { 59 rand.Seed(2) 60 for i := 0; i < 1000; i++ { 61 size := 1 + (i / 5) 62 { 63 x := RandomBool(size, 0.5) 64 r1 := make([]float64, size) 65 r2 := make([]float64, size) 66 FromBool_AVX2_F64(r1, x) 67 FromBool_Go(r2, x) 68 require.InDeltaSlice(t, r1, r2, 0.001) 69 } 70 { 71 x := RandomBool(size, 0.5) 72 r1 := make([]float32, size) 73 r2 := make([]float32, size) 74 FromBool_AVX2_F32(r1, x) 75 FromBool_Go(r2, x) 76 require.InDeltaSlice(t, r1, r2, 0.001) 77 } 78 { 79 x := Random[int64](size) 80 r1 := make([]float64, size) 81 r2 := make([]float64, size) 82 FromInt64_AVX2_F64(r1, x) 83 FromNumber_Go(r2, x) 84 require.InDeltaSlice(t, r1, r2, 0.001) 85 } 86 { 87 x := Random[int64](size) 88 r1 := make([]float32, size) 89 r2 := make([]float32, size) 90 FromInt64_AVX2_F32(r1, x) 91 FromNumber_Go(r2, x) 92 require.InDeltaSlice(t, r1, r2, 0.001) 93 } 94 { 95 x := Random[int32](size) 96 r1 := make([]float64, size) 97 r2 := make([]float64, size) 98 FromInt32_AVX2_F64(r1, x) 99 FromNumber_Go(r2, x) 100 require.InDeltaSlice(t, r1, r2, 0.001) 101 } 102 { 103 x := Random[int32](size) 104 r1 := make([]float32, size) 105 r2 := make([]float32, size) 106 FromInt32_AVX2_F32(r1, x) 107 FromNumber_Go(r2, x) 108 require.InDeltaSlice(t, r1, r2, 0.001) 109 } 110 } 111 } 112 113 func TestTo(t *testing.T) { 114 rand.Seed(2) 115 for i := 0; i < 1000; i++ { 116 size := 1 + (i / 5) 117 { 118 x := Random[float64](size) 119 r1 := make([]bool, size) 120 r2 := make([]bool, size) 121 ToBool_AVX2_F64(r1, x) 122 ToBool_Go(r2, x) 123 require.Equal(t, r1, r2) 124 } 125 { 126 x := Random[float32](size) 127 r1 := make([]bool, size) 128 r2 := make([]bool, size) 129 ToBool_AVX2_F32(r1, x) 130 ToBool_Go(r2, x) 131 require.Equal(t, r1, r2) 132 } 133 { 134 x := Random[float64](size) 135 r1 := make([]int64, size) 136 r2 := make([]int64, size) 137 ToInt64_AVX2_F64(r1, x) 138 ToNumber_Go(r2, x) 139 require.InDeltaSlice(t, r1, r2, 0.001) 140 } 141 { 142 x := Random[float32](size) 143 r1 := make([]int64, size) 144 r2 := make([]int64, size) 145 ToInt64_AVX2_F32(r1, x) 146 ToNumber_Go(r2, x) 147 require.InDeltaSlice(t, r1, r2, 0.001) 148 } 149 { 150 x := Random[float64](size) 151 r1 := make([]int32, size) 152 r2 := make([]int32, size) 153 ToInt32_AVX2_F64(r1, x) 154 ToNumber_Go(r2, x) 155 require.InDeltaSlice(t, r1, r2, 0.001) 156 } 157 { 158 x := Random[float32](size) 159 r1 := make([]int32, size) 160 r2 := make([]int32, size) 161 ToInt32_AVX2_F32(r1, x) 162 ToNumber_Go(r2, x) 163 require.InDeltaSlice(t, r1, r2, 0.001) 164 } 165 } 166 } 167 168 func BenchmarkRepeat(b *testing.B) { 169 for _, size := range sizes { 170 x := make([]float64, size) 171 a := rand.NormFloat64() 172 x32 := make([]float32, size) 173 a32 := float32(rand.NormFloat64()) 174 175 b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) { 176 for i := 0; i < b.N; i++ { 177 Repeat_Go(x, a, size) 178 } 179 }) 180 b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) { 181 for i := 0; i < b.N; i++ { 182 Repeat_Go(x32, a32, size) 183 } 184 }) 185 b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) { 186 for i := 0; i < b.N; i++ { 187 Repeat_AVX2_F64(x, a, size) 188 } 189 }) 190 b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) { 191 for i := 0; i < b.N; i++ { 192 Repeat_AVX2_F32(x32, a32, size) 193 } 194 }) 195 } 196 } 197 198 func BenchmarkRange(b *testing.B) { 199 for _, size := range sizes { 200 x := make([]float64, size) 201 a := rand.Float64() * 1e6 202 x32 := make([]float32, size) 203 a32 := rand.Float32() * 1e6 204 205 b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) { 206 for i := 0; i < b.N; i++ { 207 Range_Go(x, a, size) 208 } 209 }) 210 b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) { 211 for i := 0; i < b.N; i++ { 212 Range_Go(x32, a32, size) 213 } 214 }) 215 b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) { 216 for i := 0; i < b.N; i++ { 217 Range_AVX2_F64(x, a, size) 218 } 219 }) 220 b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) { 221 for i := 0; i < b.N; i++ { 222 Range_AVX2_F32(x32, a32, size) 223 } 224 }) 225 } 226 } 227 228 func BenchmarkFrom(b *testing.B) { 229 for _, size := range sizes { 230 x := make([]float64, size) 231 x32 := make([]float32, size) 232 y := Random[int64](size) 233 y32 := Random[int32](size) 234 _ = y 235 236 b.Run(fmt.Sprintf("go_f64_int32_%d", size), func(b *testing.B) { 237 for i := 0; i < b.N; i++ { 238 FromNumber_Go(x, y32) 239 } 240 }) 241 b.Run(fmt.Sprintf("go_f32_int32_%d", size), func(b *testing.B) { 242 for i := 0; i < b.N; i++ { 243 FromNumber_Go(x32, y32) 244 } 245 }) 246 b.Run(fmt.Sprintf("avx2_f64_int32_%d", size), func(b *testing.B) { 247 for i := 0; i < b.N; i++ { 248 FromInt32_AVX2_F64(x, y32) 249 } 250 }) 251 b.Run(fmt.Sprintf("avx2_f32_int32_%d", size), func(b *testing.B) { 252 for i := 0; i < b.N; i++ { 253 FromInt32_AVX2_F32(x32, y32) 254 } 255 }) 256 } 257 } 258 259 func BenchmarkTo(b *testing.B) { 260 for _, size := range sizes { 261 x := Random[int64](size) 262 x32 := Random[int32](size) 263 y := make([]float64, size) 264 y32 := make([]float32, size) 265 _ = x 266 267 b.Run(fmt.Sprintf("go_f64_int32_%d", size), func(b *testing.B) { 268 for i := 0; i < b.N; i++ { 269 ToNumber_Go(x32, y) 270 } 271 }) 272 b.Run(fmt.Sprintf("go_f32_int32_%d", size), func(b *testing.B) { 273 for i := 0; i < b.N; i++ { 274 ToNumber_Go(x32, y32) 275 } 276 }) 277 b.Run(fmt.Sprintf("avx2_f64_int32_%d", size), func(b *testing.B) { 278 for i := 0; i < b.N; i++ { 279 ToInt32_AVX2_F64(x32, y) 280 } 281 }) 282 b.Run(fmt.Sprintf("avx2_f32_int32_%d", size), func(b *testing.B) { 283 for i := 0; i < b.N; i++ { 284 ToInt32_AVX2_F32(x32, y32) 285 } 286 }) 287 } 288 } 289 290 var zeros [512]float64 291 292 func BenchmarkZeros(b *testing.B) { 293 for _, size := range sizes { 294 x := Random[float64](size) 295 296 b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) { 297 for i := 0; i < b.N; i++ { 298 Zeros_Go(x, size) 299 } 300 }) 301 b.Run(fmt.Sprintf("go_f64_repeat_%d", size), func(b *testing.B) { 302 for i := 0; i < b.N; i++ { 303 bytes.Repeat([]byte{0}, size) 304 } 305 }) 306 b.Run(fmt.Sprintf("go_f64_copy_%d", size), func(b *testing.B) { 307 for i := 0; i < b.N; i++ { 308 j := 0 309 for ; j < size&int(-512); j += 512 { 310 copy(x[j:j+512], zeros[:]) 311 } 312 copy(x[j:], zeros[:len(x)-j]) 313 } 314 }) 315 b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) { 316 for i := 0; i < b.N; i++ { 317 Repeat_AVX2_F64(x, 0, size) 318 } 319 }) 320 } 321 }