gitee.com/quant1x/num@v0.3.2/repeat_test.go (about) 1 package num 2 3 import ( 4 "bytes" 5 "fmt" 6 "math" 7 "testing" 8 ) 9 10 func TestRepeat(t *testing.T) { 11 f32 := float32(1) 12 f64 := float64(1) 13 14 n := 10 15 fs32 := Repeat(f32, n) 16 fmt.Println(fs32) 17 fs64 := Repeat(f64, n) 18 fmt.Println(fs64) 19 } 20 21 func Test_v1Repeat_0(t *testing.T) { 22 count := 5 23 t1 := make([]byte, count) 24 for i := 0; i < count; i++ { 25 t1[i] = 'a' 26 } 27 t2 := bytes.Repeat(t1, count) 28 fmt.Println(t2) 29 } 30 31 func Test_v1Repeat_1(t *testing.T) { 32 f := 1.1 33 fmt.Println(math.Float64bits(f)) 34 x := v2Repeat(f, 5) 35 fmt.Println(x) 36 } 37 38 func Test_v4Repeat(t *testing.T) { 39 f := 1.1 40 x := v5Repeat(f, 5) 41 fmt.Println(x) 42 } 43 44 const ( 45 benchRepeatTimes = 5000 46 ) 47 48 func BenchmarkRepeat_release(b *testing.B) { 49 for n := 0; n < b.N; n++ { 50 Repeat[float64](1.1, benchRepeatTimes) 51 } 52 } 53 54 func BenchmarkRepeat_release_avx2_float32(b *testing.B) { 55 useAvx2 := GetAvx2Enabled() 56 SetAvx2Enabled(true) 57 defer SetAvx2Enabled(useAvx2) 58 for n := 0; n < b.N; n++ { 59 Repeat[float32](1.1, benchRepeatTimes) 60 } 61 } 62 func BenchmarkRepeat_release_avx2_float64(b *testing.B) { 63 useAvx2 := GetAvx2Enabled() 64 SetAvx2Enabled(true) 65 defer SetAvx2Enabled(useAvx2) 66 for n := 0; n < b.N; n++ { 67 Repeat[float64](1.1, benchRepeatTimes) 68 } 69 } 70 71 func BenchmarkRepeat_release_noavx2_float32(b *testing.B) { 72 useAvx2 := GetAvx2Enabled() 73 SetAvx2Enabled(false) 74 defer SetAvx2Enabled(useAvx2) 75 for n := 0; n < b.N; n++ { 76 Repeat[float32](1.1, benchRepeatTimes) 77 } 78 } 79 func BenchmarkRepeat_release_noavx2_float64(b *testing.B) { 80 useAvx2 := GetAvx2Enabled() 81 SetAvx2Enabled(false) 82 defer SetAvx2Enabled(useAvx2) 83 for n := 0; n < b.N; n++ { 84 Repeat[float64](1.1, benchRepeatTimes) 85 } 86 } 87 88 func BenchmarkRepeat_v1_avx2_float32(b *testing.B) { 89 useAvx2 := GetAvx2Enabled() 90 SetAvx2Enabled(true) 91 defer SetAvx2Enabled(useAvx2) 92 for n := 0; n < b.N; n++ { 93 v1Repeat[float32](1.1, benchRepeatTimes) 94 } 95 } 96 func BenchmarkRepeat_v1_avx2_float64(b *testing.B) { 97 useAvx2 := GetAvx2Enabled() 98 SetAvx2Enabled(true) 99 defer SetAvx2Enabled(useAvx2) 100 for n := 0; n < b.N; n++ { 101 v1Repeat[float64](1.1, benchRepeatTimes) 102 } 103 } 104 105 func BenchmarkRepeat_v1_noavx2_float32(b *testing.B) { 106 useAvx2 := GetAvx2Enabled() 107 SetAvx2Enabled(false) 108 defer SetAvx2Enabled(useAvx2) 109 for n := 0; n < b.N; n++ { 110 v1Repeat[float32](1.1, benchRepeatTimes) 111 } 112 } 113 func BenchmarkRepeat_v1_noavx2_float64(b *testing.B) { 114 useAvx2 := GetAvx2Enabled() 115 SetAvx2Enabled(false) 116 defer SetAvx2Enabled(useAvx2) 117 for n := 0; n < b.N; n++ { 118 v1Repeat[float64](1.1, benchRepeatTimes) 119 } 120 } 121 122 func BenchmarkRepeat_v2_noavx2_float32(b *testing.B) { 123 useAvx2 := GetAvx2Enabled() 124 SetAvx2Enabled(false) 125 defer SetAvx2Enabled(useAvx2) 126 for n := 0; n < b.N; n++ { 127 v2Repeat[float32](1.1, benchRepeatTimes) 128 } 129 } 130 131 func BenchmarkRepeat_v2_noavx2_float64(b *testing.B) { 132 useAvx2 := GetAvx2Enabled() 133 SetAvx2Enabled(false) 134 defer SetAvx2Enabled(useAvx2) 135 for n := 0; n < b.N; n++ { 136 v2Repeat[float64](1.1, benchRepeatTimes) 137 } 138 } 139 140 func BenchmarkRepeat_v2_float32_avx2(b *testing.B) { 141 useAvx2 := GetAvx2Enabled() 142 SetAvx2Enabled(true) 143 defer SetAvx2Enabled(useAvx2) 144 for n := 0; n < b.N; n++ { 145 v2Repeat[float32](1.1, benchRepeatTimes) 146 } 147 } 148 149 func BenchmarkRepeat_v2_float64_avx2(b *testing.B) { 150 useAvx2 := GetAvx2Enabled() 151 SetAvx2Enabled(true) 152 defer SetAvx2Enabled(useAvx2) 153 for n := 0; n < b.N; n++ { 154 v2Repeat[float64](1.1, benchRepeatTimes) 155 } 156 } 157 158 func BenchmarkRepeat_v3_float32(b *testing.B) { 159 for n := 0; n < b.N; n++ { 160 v3Repeat[float32](1.1, benchRepeatTimes) 161 } 162 } 163 164 func BenchmarkRepeat_v3_float64(b *testing.B) { 165 for n := 0; n < b.N; n++ { 166 v3Repeat[float64](1.1, benchRepeatTimes) 167 } 168 } 169 170 func BenchmarkRepeat_v4_float32(b *testing.B) { 171 for n := 0; n < b.N; n++ { 172 v4Repeat[float32](1.1, benchRepeatTimes) 173 } 174 } 175 176 func BenchmarkRepeat_v4_float64(b *testing.B) { 177 for n := 0; n < b.N; n++ { 178 v4Repeat[float64](1.1, benchRepeatTimes) 179 } 180 } 181 182 func BenchmarkRepeat_v5_float32(b *testing.B) { 183 for n := 0; n < b.N; n++ { 184 v5Repeat[float32](1.1, benchRepeatTimes) 185 } 186 } 187 188 func BenchmarkRepeat_v5_float64(b *testing.B) { 189 for n := 0; n < b.N; n++ { 190 v5Repeat[float64](1.1, benchRepeatTimes) 191 } 192 }