gitee.com/quant1x/num@v0.3.2/internal/functions/comparison_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 TestCompare(t *testing.T) { 12 rand.Seed(2) 13 for i := 0; i < 1000; i++ { 14 size := 1 + (i / 5) 15 { 16 x := Random[float64](size) 17 y := Random[float64](size) 18 x[size/2] = y[size/2] 19 r1 := make([]bool, size) 20 r2 := make([]bool, size) 21 22 Lt_Go(r1, x, y) 23 Lt_AVX2_F64(r2, x, y) 24 require.Equal(t, r1, r2) 25 } 26 { 27 x := Random[float32](size) 28 y := Random[float32](size) 29 x[size/2] = y[size/2] 30 r1 := make([]bool, size) 31 r2 := make([]bool, size) 32 33 Lte_Go(r1, x, y) 34 Lte_AVX2_F32(r2, x, y) 35 require.Equal(t, r1, r2) 36 } 37 { 38 x := Random[float64](size) 39 y := Random[float64](size) 40 x[size/2] = y[size/2] 41 r1 := make([]bool, size) 42 r2 := make([]bool, size) 43 44 Gte_Go(r1, x, y) 45 Gte_AVX2_F64(r2, x, y) 46 require.Equal(t, r1, r2) 47 } 48 { 49 x := Random[float64](size) 50 y := Random[float64](size) 51 x[size/2] = y[size/2] 52 r1 := make([]bool, size) 53 r2 := make([]bool, size) 54 55 Eq_Go(r1, x, y) 56 Eq_AVX2_F64(r2, x, y) 57 require.Equal(t, r1, r2) 58 } 59 { 60 x := Random[float64](size) 61 y := Random[float64](size) 62 x[size/2] = y[size/2] 63 r1 := make([]bool, size) 64 r2 := make([]bool, size) 65 66 Neq_Go(r1, x, y) 67 Neq_AVX2_F64(r2, x, y) 68 require.Equal(t, r1, r2) 69 } 70 } 71 } 72 73 func BenchmarkLt(b *testing.B) { 74 for _, size := range sizes[3:] { 75 x := Random[float64](size) 76 y := Random[float64](size) 77 x32 := Random[float32](size) 78 y32 := Random[float32](size) 79 dst := make([]bool, size) 80 81 b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) { 82 for i := 0; i < b.N; i++ { 83 b.StopTimer() 84 dst := slices.Clone(dst) 85 b.StartTimer() 86 Lt_Go(dst, x, y) 87 } 88 }) 89 b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) { 90 for i := 0; i < b.N; i++ { 91 b.StopTimer() 92 dst := slices.Clone(dst) 93 b.StartTimer() 94 Lt_Go(dst, x32, y32) 95 } 96 }) 97 b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) { 98 for i := 0; i < b.N; i++ { 99 b.StopTimer() 100 dst := slices.Clone(dst) 101 b.StartTimer() 102 Lt_AVX2_F64(dst, x, y) 103 } 104 }) 105 b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) { 106 for i := 0; i < b.N; i++ { 107 b.StopTimer() 108 dst := slices.Clone(dst) 109 b.StartTimer() 110 Lt_AVX2_F32(dst, x32, y32) 111 } 112 }) 113 } 114 }