gitee.com/quant1x/num@v0.3.2/internal/functions/find_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  func TestFind(t *testing.T) {
    11  	rand.Seed(2)
    12  	for i := 1; i < 1000; i++ {
    13  		size := 1 + (i / 5)
    14  		{
    15  			x := Random[float64](size)
    16  			a := x[rand.Intn(len(x))]
    17  			r1 := Find_Go(x, a)
    18  			r2 := Find_AVX2_F64(x, a)
    19  			require.Equalf(t, r1, r2, "x: %v, a: %v", x, a)
    20  		}
    21  		{
    22  			x := Random[float32](size)
    23  			a := x[rand.Intn(len(x))]
    24  			r1 := Find_Go(x, a)
    25  			r2 := Find_AVX2_F32(x, a)
    26  			require.Equalf(t, r1, r2, "x: %v, a: %v", x, a)
    27  		}
    28  	}
    29  }
    30  
    31  func BenchmarkFind(b *testing.B) {
    32  	for _, size := range sizes {
    33  		x := Random[float64](size)
    34  		x32 := Random[float32](size)
    35  
    36  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
    37  			for i := 0; i < b.N; i++ {
    38  				Find_Go(x, x[len(x)-1])
    39  			}
    40  		})
    41  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
    42  			for i := 0; i < b.N; i++ {
    43  				Find_Go(x32, x32[len(x32)-1])
    44  			}
    45  		})
    46  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
    47  			for i := 0; i < b.N; i++ {
    48  				Find_AVX2_F64(x, x[len(x)-1])
    49  			}
    50  		})
    51  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
    52  			for i := 0; i < b.N; i++ {
    53  				Find_AVX2_F32(x32, x32[len(x32)-1])
    54  			}
    55  		})
    56  	}
    57  }