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

     1  package g_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/enetx/g"
     8  )
     9  
    10  func genText() g.String {
    11  	text := g.String("").Builder()
    12  	for range 10000 {
    13  		text.Write(g.String("").Random(1000)).WriteByte('\n')
    14  	}
    15  
    16  	return text.String()
    17  }
    18  
    19  func BenchmarkSplit(b *testing.B) {
    20  	text := genText()
    21  	b.ResetTimer()
    22  
    23  	for n := 0; n < b.N; n++ {
    24  		text.Split("\n").Collect()
    25  	}
    26  }
    27  
    28  func BenchmarkSplitStd(b *testing.B) {
    29  	text := genText().Std()
    30  	b.ResetTimer()
    31  
    32  	for n := 0; n < b.N; n++ {
    33  		strings.Split(text, "\n")
    34  	}
    35  }
    36  
    37  func BenchmarkFields(b *testing.B) {
    38  	text := genText()
    39  	b.ResetTimer()
    40  
    41  	for n := 0; n < b.N; n++ {
    42  		text.Fields().Collect()
    43  	}
    44  }
    45  
    46  func BenchmarkFieldsStd(b *testing.B) {
    47  	text := genText().Std()
    48  	b.ResetTimer()
    49  
    50  	for n := 0; n < b.N; n++ {
    51  		strings.Fields(text)
    52  	}
    53  }