gitee.com/quant1x/num@v0.3.2/internal/functions/max_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 TestMax(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  			r1 := Max_AVX2_F64(x)
    18  			r2 := Max_Go(x)
    19  			require.InDelta(t, r1, r2, 0.001)
    20  		}
    21  		{
    22  			x := Random[float32](size)
    23  			r1 := Max_AVX2_F32(x)
    24  			r2 := Max_Go(x)
    25  			require.InDelta(t, r1, r2, 0.001)
    26  		}
    27  		{
    28  			x := Random[float64](size)
    29  			r1 := ArgMax_AVX2_F64(x)
    30  			r2 := ArgMax_Go(x)
    31  			require.Equal(t, r1, r2)
    32  		}
    33  		{
    34  			x := Random[float32](size)
    35  			r1 := ArgMax_AVX2_F32(x)
    36  			r2 := ArgMax_Go(x)
    37  			require.Equal(t, r1, r2)
    38  		}
    39  	}
    40  }
    41  
    42  func TestMaximum(t *testing.T) {
    43  	rand.Seed(2)
    44  	for i := 0; i < 1000; i++ {
    45  		size := 1 + (i / 5)
    46  		{
    47  			x := Random[float64](size)
    48  			y := Random[float64](size)
    49  			x1 := slices.Clone(x)
    50  			Maximum_AVX2_F64(x, y)
    51  			Maximum_Go(x1, y)
    52  			require.InEpsilonSlice(t, x, x1, 0.001)
    53  		}
    54  		{
    55  			x := Random[float32](size)
    56  			y := Random[float32](size)
    57  			x1 := slices.Clone(x)
    58  			Maximum_AVX2_F32(x, y)
    59  			Maximum_Go(x1, y)
    60  			require.InDeltaSlice(t, x, x1, 0.001)
    61  		}
    62  		{
    63  			x := Random[float64](size)
    64  			a := rand.Float64()
    65  			x1 := slices.Clone(x)
    66  			MaximumNumber_AVX2_F64(x, a)
    67  			MaximumNumber_Go(x1, a)
    68  			require.InEpsilonSlice(t, x, x1, 0.001)
    69  		}
    70  		{
    71  			x := Random[float32](size)
    72  			a := rand.Float32()
    73  			x1 := slices.Clone(x)
    74  			MaximumNumber_AVX2_F32(x, a)
    75  			MaximumNumber_Go(x1, a)
    76  			require.InEpsilonSlice(t, x, x1, 0.001)
    77  		}
    78  	}
    79  }
    80  
    81  func BenchmarkMax(b *testing.B) {
    82  	for _, size := range sizes {
    83  		x := Random[float64](size)
    84  		x32 := Random[float32](size)
    85  
    86  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
    87  			for i := 0; i < b.N; i++ {
    88  				Max_Go(x)
    89  			}
    90  		})
    91  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
    92  			for i := 0; i < b.N; i++ {
    93  				Max_Go(x32)
    94  			}
    95  		})
    96  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
    97  			for i := 0; i < b.N; i++ {
    98  				Max_AVX2_F64(x)
    99  			}
   100  		})
   101  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   102  			for i := 0; i < b.N; i++ {
   103  				Max_AVX2_F32(x32)
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func BenchmarkMaximum(b *testing.B) {
   110  	for _, size := range sizes[3:] {
   111  		x := Random[float64](size)
   112  		y := Random[float64](size)
   113  		x32 := Random[float32](size)
   114  		y32 := Random[float32](size)
   115  
   116  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
   117  			for i := 0; i < b.N; i++ {
   118  				b.StopTimer()
   119  				x := slices.Clone(x)
   120  				b.StartTimer()
   121  				Maximum_Go(x, y)
   122  			}
   123  		})
   124  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   125  			for i := 0; i < b.N; i++ {
   126  				b.StopTimer()
   127  				x32 := slices.Clone(x32)
   128  				b.StartTimer()
   129  				Maximum_Go(x32, y32)
   130  			}
   131  		})
   132  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   133  			for i := 0; i < b.N; i++ {
   134  				b.StopTimer()
   135  				x := slices.Clone(x)
   136  				b.StartTimer()
   137  				Maximum_AVX2_F64(x, y)
   138  			}
   139  		})
   140  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   141  			for i := 0; i < b.N; i++ {
   142  				b.StopTimer()
   143  				x32 := slices.Clone(x32)
   144  				b.StartTimer()
   145  				Maximum_AVX2_F32(x32, y32)
   146  			}
   147  		})
   148  	}
   149  }
   150  
   151  func BenchmarkArgMax(b *testing.B) {
   152  	for _, size := range sizes {
   153  		x := Random[float64](size)
   154  		x32 := Random[float32](size)
   155  
   156  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
   157  			for i := 0; i < b.N; i++ {
   158  				ArgMax_Go(x)
   159  			}
   160  		})
   161  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   162  			for i := 0; i < b.N; i++ {
   163  				ArgMax_Go(x32)
   164  			}
   165  		})
   166  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   167  			for i := 0; i < b.N; i++ {
   168  				ArgMax_AVX2_F64(x)
   169  			}
   170  		})
   171  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   172  			for i := 0; i < b.N; i++ {
   173  				ArgMax_AVX2_F32(x32)
   174  			}
   175  		})
   176  	}
   177  }