gitee.com/quant1x/num@v0.3.2/internal/functions/arithmetic_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 TestAdd(t *testing.T) {
    12  	rand.Seed(2)
    13  	for i := 0; i < 1000; i++ {
    14  		size := 1 + (i / 5)
    15  		{
    16  			a := rand.NormFloat64() * 100
    17  			y := Random[float64](size)
    18  			r1 := make([]float64, size)
    19  			r2 := make([]float64, size)
    20  
    21  			Add_Go(r1, y)
    22  			Add_AVX2_F64(r2, y)
    23  			require.InDeltaSlice(t, r1, r2, 0.001)
    24  
    25  			AddNumber_Go(r1, a)
    26  			AddNumber_AVX2_F64(r2, a)
    27  			require.InDeltaSlice(t, r1, r2, 0.001)
    28  		}
    29  		{
    30  			a := float32(rand.NormFloat64() * 100)
    31  			y := Random[float32](size)
    32  			r1 := make([]float32, size)
    33  			r2 := make([]float32, size)
    34  
    35  			Add_Go(r1, y)
    36  			Add_AVX2_F32(r2, y)
    37  			require.InDeltaSlice(t, r1, r2, 0.001)
    38  
    39  			AddNumber_Go(r1, a)
    40  			AddNumber_AVX2_F32(r2, a)
    41  			require.InDeltaSlice(t, r1, r2, 0.001)
    42  		}
    43  	}
    44  }
    45  
    46  func TestAbsNegInv(t *testing.T) {
    47  	rand.Seed(2)
    48  	for i := 0; i < 1000; i++ {
    49  		size := 1 + (i / 5)
    50  		{
    51  			r1 := Random[float64](size)
    52  			r2 := slices.Clone(r1)
    53  
    54  			Neg_Go(r1)
    55  			Neg_AVX2_F64(r2)
    56  			require.InDeltaSlice(t, r1, r2, 0.001)
    57  
    58  			Inv_Go(r1)
    59  			Inv_AVX2_F64(r2)
    60  			require.InDeltaSlice(t, r1, r2, 0.001)
    61  
    62  			Abs_Go_F64(r1)
    63  			Abs_AVX2_F64(r2)
    64  			require.InDeltaSlice(t, r1, r2, 0.001)
    65  		}
    66  		{
    67  			r1 := Random[float32](size)
    68  			r2 := slices.Clone(r1)
    69  
    70  			Neg_Go(r1)
    71  			Neg_AVX2_F32(r2)
    72  			require.InDeltaSlice(t, r1, r2, 0.01)
    73  
    74  			r1 = Random[float32](size)
    75  			r2 = slices.Clone(r1)
    76  
    77  			Inv_Go(r1)
    78  			Inv_AVX2_F32(r2)
    79  			require.InDeltaSlice(t, r1, r2, 0.01)
    80  
    81  			r1 = Random[float32](size)
    82  			r2 = slices.Clone(r1)
    83  
    84  			Abs_Go_F32(r1)
    85  			Abs_AVX2_F32(r2)
    86  			require.InDeltaSlice(t, r1, r2, 0.01)
    87  		}
    88  	}
    89  }
    90  
    91  func BenchmarkAdd(b *testing.B) {
    92  	for _, size := range sizes[3:] {
    93  		x := Random[float64](size)
    94  		y := Random[float64](size)
    95  		x32 := Random[float32](size)
    96  		y32 := Random[float32](size)
    97  
    98  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
    99  			for i := 0; i < b.N; i++ {
   100  				b.StopTimer()
   101  				x := slices.Clone(x)
   102  				b.StartTimer()
   103  				Add_Go(x, y)
   104  			}
   105  		})
   106  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   107  			for i := 0; i < b.N; i++ {
   108  				b.StopTimer()
   109  				x32 := slices.Clone(x32)
   110  				b.StartTimer()
   111  				Add_Go(x32, y32)
   112  			}
   113  		})
   114  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   115  			for i := 0; i < b.N; i++ {
   116  				b.StopTimer()
   117  				x := slices.Clone(x)
   118  				b.StartTimer()
   119  				Add_AVX2_F64(x, y)
   120  			}
   121  		})
   122  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   123  			for i := 0; i < b.N; i++ {
   124  				b.StopTimer()
   125  				x32 := slices.Clone(x32)
   126  				b.StartTimer()
   127  				Add_AVX2_F32(x32, y32)
   128  			}
   129  		})
   130  	}
   131  }
   132  
   133  func BenchmarkMul(b *testing.B) {
   134  	for _, size := range sizes[3:] {
   135  		x := Random[float64](size)
   136  		y := Random[float64](size)
   137  		x32 := Random[float32](size)
   138  		y32 := Random[float32](size)
   139  
   140  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
   141  			for i := 0; i < b.N; i++ {
   142  				b.StopTimer()
   143  				x := slices.Clone(x)
   144  				b.StartTimer()
   145  				Mul_Go(x, y)
   146  			}
   147  		})
   148  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   149  			for i := 0; i < b.N; i++ {
   150  				b.StopTimer()
   151  				x32 := slices.Clone(x32)
   152  				b.StartTimer()
   153  				Mul_Go(x32, y32)
   154  			}
   155  		})
   156  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   157  			for i := 0; i < b.N; i++ {
   158  				b.StopTimer()
   159  				x := slices.Clone(x)
   160  				b.StartTimer()
   161  				Mul_AVX2_F64(x, y)
   162  			}
   163  		})
   164  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   165  			for i := 0; i < b.N; i++ {
   166  				b.StopTimer()
   167  				x32 := slices.Clone(x32)
   168  				b.StartTimer()
   169  				Mul_AVX2_F32(x32, y32)
   170  			}
   171  		})
   172  	}
   173  }
   174  
   175  func BenchmarkAbs(b *testing.B) {
   176  	for _, size := range sizes[3:] {
   177  		x := Random[float64](size)
   178  		x32 := Random[float32](size)
   179  
   180  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
   181  			for i := 0; i < b.N; i++ {
   182  				b.StopTimer()
   183  				x := slices.Clone(x)
   184  				b.StartTimer()
   185  				Abs_Go_F64(x)
   186  			}
   187  		})
   188  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   189  			for i := 0; i < b.N; i++ {
   190  				b.StopTimer()
   191  				x32 := slices.Clone(x32)
   192  				b.StartTimer()
   193  				Abs_Go_F32(x32)
   194  			}
   195  		})
   196  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   197  			for i := 0; i < b.N; i++ {
   198  				b.StopTimer()
   199  				x := slices.Clone(x)
   200  				b.StartTimer()
   201  				Abs_AVX2_F64(x)
   202  			}
   203  		})
   204  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   205  			for i := 0; i < b.N; i++ {
   206  				b.StopTimer()
   207  				x32 := slices.Clone(x32)
   208  				b.StartTimer()
   209  				Abs_AVX2_F32(x32)
   210  			}
   211  		})
   212  	}
   213  }