github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/benchmark/convert/convert_benchmark_test.go (about)

     1  package convert
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/m4gshm/gollections/collection"
     8  	"github.com/m4gshm/gollections/collection/immutable/vector"
     9  	"github.com/m4gshm/gollections/collection/mutable"
    10  	mvector "github.com/m4gshm/gollections/collection/mutable/vector"
    11  	"github.com/m4gshm/gollections/convert"
    12  	"github.com/m4gshm/gollections/convert/ptr"
    13  	"github.com/m4gshm/gollections/loop"
    14  	"github.com/m4gshm/gollections/slice"
    15  	"github.com/m4gshm/gollections/slice/range_"
    16  )
    17  
    18  var (
    19  	toString = func(i int) string { return fmt.Sprintf("%d", i) }
    20  	addTail  = func(s string) string { return s + "_tail" }
    21  	even     = func(v int) bool { return v%2 == 0 }
    22  	max      = 100000
    23  	values   = range_.Closed(1, max)
    24  )
    25  
    26  func Benchmark_Convert_Slice(b *testing.B) {
    27  	op := convert.And(toString, addTail)
    28  	var s []string
    29  	b.ResetTimer()
    30  	for i := 0; i < b.N; i++ {
    31  		s = slice.Convert(values, op)
    32  	}
    33  	_ = len(s)
    34  	b.StopTimer()
    35  }
    36  
    37  func Benchmark_Convert_Slice_EveryElement(b *testing.B) {
    38  	op := convert.And(toString, addTail)
    39  	var s []string
    40  	b.ResetTimer()
    41  	for i := 0; i < b.N; i++ {
    42  		s = make([]string, len(values))
    43  		for i, v := range values {
    44  			s[i] = op(v)
    45  		}
    46  	}
    47  	_ = s
    48  	b.StopTimer()
    49  }
    50  
    51  func Benchmark_Convert_Loop(b *testing.B) {
    52  	op := convert.And(toString, addTail)
    53  	var s []string
    54  	b.ResetTimer()
    55  	for i := 0; i < b.N; i++ {
    56  		it := slice.NewHead(values)
    57  		s = loop.SliceCap(loop.Convert(it.Next, op), len(values))
    58  	}
    59  	_ = s
    60  	b.StopTimer()
    61  }
    62  
    63  func Benchmark_Convert_ImmutableVector_Iterable(b *testing.B) {
    64  	concat := convert.And(toString, addTail)
    65  	items := vector.Of(values...)
    66  	var s []string
    67  	b.ResetTimer()
    68  	for i := 0; i < b.N; i++ {
    69  		s = collection.Convert(items, concat).Slice()
    70  	}
    71  	_ = s
    72  
    73  	b.StopTimer()
    74  }
    75  
    76  func Benchmark_Convert_ImmutableVector_Iterable_Append(b *testing.B) {
    77  	concat := convert.And(toString, addTail)
    78  	items := vector.Of(values...)
    79  	var s []string
    80  	b.ResetTimer()
    81  	for i := 0; i < b.N; i++ {
    82  		s = collection.Convert(items, concat).Append(make([]string, 0, len(values)))
    83  	}
    84  	_ = s
    85  
    86  	b.StopTimer()
    87  }
    88  
    89  func Benchmark_Convert_ImmutableVector(b *testing.B) {
    90  	concat := convert.And(toString, addTail)
    91  	items := vector.Of(values...)
    92  	var s []string
    93  	b.ResetTimer()
    94  	for i := 0; i < b.N; i++ {
    95  		s = vector.Convert(items, concat).Slice()
    96  	}
    97  	_ = s
    98  	b.StopTimer()
    99  }
   100  
   101  func Benchmark_Convert_ImmutableVector_Append(b *testing.B) {
   102  	concat := convert.And(toString, addTail)
   103  	items := vector.Of(values...)
   104  	var s []string
   105  	b.ResetTimer()
   106  	for i := 0; i < b.N; i++ {
   107  		s = vector.Convert(items, concat).Append(make([]string, 0, len(values)))
   108  	}
   109  	_ = s
   110  	b.StopTimer()
   111  }
   112  
   113  func Benchmark_Convert_ImmutableVector_Head_Loop(b *testing.B) {
   114  	concat := convert.And(toString, addTail)
   115  	items := vector.Of(values...)
   116  	var s []string
   117  	b.ResetTimer()
   118  	for i := 0; i < b.N; i++ {
   119  		s = loop.SliceCap(loop.Convert(ptr.Of(items.Head()).Next, concat), len(values))
   120  	}
   121  	_ = s
   122  	b.StopTimer()
   123  }
   124  
   125  func Benchmark_Convert_ImmutableVector_ForEach_To_MutableVector(b *testing.B) {
   126  	concat := convert.And(toString, addTail)
   127  	items := vector.Of(values...)
   128  	c := len(values)
   129  	var s *mutable.Vector[string]
   130  	b.ResetTimer()
   131  	for i := 0; i < b.N; i++ {
   132  		s = mvector.NewCap[string](c)
   133  		items.ForEach(func(element int) { s.Add(concat(element)) })
   134  	}
   135  	_ = s
   136  	b.StopTimer()
   137  }