github.com/enetx/g@v1.0.80/tests/slice_benchmark_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enetx/g"
     7  	"github.com/enetx/g/f"
     8  )
     9  
    10  // go test -bench=. -benchmem -count=4
    11  
    12  func genSlice() g.Slice[g.String] {
    13  	slice := g.NewSlice[g.String](0, 10000)
    14  	for i := range 10000 {
    15  		slice = slice.Append(g.NewInt(i).ToString())
    16  	}
    17  
    18  	return slice
    19  }
    20  
    21  func BenchmarkContains(b *testing.B) {
    22  	slice := genSlice()
    23  	b.ResetTimer()
    24  
    25  	for n := 0; n < b.N; n++ {
    26  		slice.Iter().Find(f.Eq(g.String("1000"))).IsSome()
    27  	}
    28  }
    29  
    30  func BenchmarkContains2(b *testing.B) {
    31  	slice := genSlice()
    32  	b.ResetTimer()
    33  
    34  	for n := 0; n < b.N; n++ {
    35  		slice.Contains("10000")
    36  	}
    37  }
    38  
    39  func BenchmarkForEach(b *testing.B) {
    40  	slice := genSlice()
    41  	b.ResetTimer()
    42  
    43  	for n := 0; n < b.N; n++ {
    44  		slice.Iter().ForEach(func(s g.String) { _ = s })
    45  	}
    46  }
    47  
    48  func BenchmarkMap(b *testing.B) {
    49  	slice := genSlice()
    50  	b.ResetTimer()
    51  
    52  	for n := 0; n < b.N; n++ {
    53  		slice.Iter().Map(func(s g.String) g.String { return s }).Collect()
    54  	}
    55  }
    56  
    57  func BenchmarkFilter(b *testing.B) {
    58  	slice := genSlice()
    59  	b.ResetTimer()
    60  
    61  	for n := 0; n < b.N; n++ {
    62  		slice.Iter().Filter(func(_ g.String) bool { return true }).Collect()
    63  	}
    64  }
    65  
    66  func BenchmarkUnique(b *testing.B) {
    67  	slice := genSlice()
    68  	b.ResetTimer()
    69  
    70  	for n := 0; n < b.N; n++ {
    71  		slice.Iter().Unique().Collect()
    72  	}
    73  }
    74  
    75  func BenchmarkDedup(b *testing.B) {
    76  	slice := genSlice()
    77  	b.ResetTimer()
    78  
    79  	for n := 0; n < b.N; n++ {
    80  		slice.Iter().Dedup().Collect()
    81  	}
    82  }