gitee.com/quant1x/num@v0.3.2/internal/functions/aggregates_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 "testing" 8 ) 9 10 var sizes = []int{ 11 10, 100, 1_000, 10_000, 100_000, 12 } 13 14 func TestSumProd(t *testing.T) { 15 rand.Seed(2) 16 for i := 0; i < 1000; i++ { 17 size := 1 + (i / 5) 18 { 19 x := Random[float64](size) 20 21 r1 := Sum_Go(x) 22 r2 := Sum_AVX2_F64(x) 23 require.InDelta(t, r1, r2, 0.001) 24 } 25 { 26 x := Random[float32](size) 27 28 r1 := Prod_Go(x) 29 r2 := Prod_AVX2_F32(x) 30 require.InDelta(t, r1, r2, 0.001) 31 } 32 } 33 } 34 35 func TestQuantile(t *testing.T) { 36 rand.Seed(2) 37 for i := 0; i < 1000; i++ { 38 size := 1 + (i / 5) 39 { 40 a := rand.Float64() 41 x := Random[float64](size) 42 43 r1 := Quantile_Go(x, a) 44 r2 := Quantile_AVX2_F64(x, a) 45 require.InDelta(t, r1, r2, 0.001) 46 } 47 } 48 } 49 50 func BenchmarkSumProd(b *testing.B) { 51 for _, size := range sizes { 52 x := Random[float64](size) 53 x32 := Random[float32](size) 54 55 b.Run(fmt.Sprintf("go_sum_f64_%d", size), func(b *testing.B) { 56 for i := 0; i < b.N; i++ { 57 Sum_Go(x) 58 } 59 }) 60 b.Run(fmt.Sprintf("go_sum_f32_%d", size), func(b *testing.B) { 61 for i := 0; i < b.N; i++ { 62 Sum_Go(x32) 63 } 64 }) 65 b.Run(fmt.Sprintf("avx2_sum_f64_%d", size), func(b *testing.B) { 66 for i := 0; i < b.N; i++ { 67 Sum_AVX2_F64(x) 68 } 69 }) 70 b.Run(fmt.Sprintf("avx2_sum_f32_%d", size), func(b *testing.B) { 71 for i := 0; i < b.N; i++ { 72 Sum_AVX2_F32(x32) 73 } 74 }) 75 b.Run(fmt.Sprintf("go_cumsum_f64_%d", size), func(b *testing.B) { 76 for i := 0; i < b.N; i++ { 77 CumSum_Go(x) 78 } 79 }) 80 b.Run(fmt.Sprintf("go_cumsum_f32_%d", size), func(b *testing.B) { 81 for i := 0; i < b.N; i++ { 82 CumSum_Go(x32) 83 } 84 }) 85 b.Run(fmt.Sprintf("avx2_cumsum_f64_%d", size), func(b *testing.B) { 86 for i := 0; i < b.N; i++ { 87 CumSum_AVX2_F64(x) 88 } 89 }) 90 b.Run(fmt.Sprintf("avx2_cumsum_f32_%d", size), func(b *testing.B) { 91 for i := 0; i < b.N; i++ { 92 CumSum_AVX2_F32(x32) 93 } 94 }) 95 } 96 } 97 98 func BenchmarkQuantile(b *testing.B) { 99 for _, size := range sizes { 100 x := Random[float64](size) 101 x32 := Random[float32](size) 102 103 b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) { 104 for i := 0; i < b.N; i++ { 105 Quantile_Go(x, 0.421) 106 } 107 }) 108 b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) { 109 for i := 0; i < b.N; i++ { 110 Quantile_Go(x32, 0.421) 111 } 112 }) 113 b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) { 114 for i := 0; i < b.N; i++ { 115 Quantile_AVX2_F64(x, 0.421) 116 } 117 }) 118 b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) { 119 for i := 0; i < b.N; i++ { 120 Quantile_AVX2_F32(x32, 0.421) 121 } 122 }) 123 } 124 }