github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/cutbyte/bench_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func Cut0(s, sep string) (before, after string, found bool) {
    10  	if i := strings.Index(s, sep); i >= 0 {
    11  		return s[:i], s[i+len(sep):], true
    12  	}
    13  	return s, "", false
    14  }
    15  
    16  func CutByte0(s string, sep byte) (before, after string, found bool) {
    17  	if i := strings.IndexByte(s, sep); i >= 0 {
    18  		return s[:i], s[i+1:], true
    19  	}
    20  	return s, "", false
    21  }
    22  
    23  func Cut1(s, sep string) (before, after string, found bool) {
    24  	if i := strings.Index(s, sep); i >= 0 {
    25  		return s[:i], s[i+len(sep):], true
    26  	}
    27  	return s, "", false
    28  }
    29  
    30  func CutByte1(s string, sep byte) (before, after string, found bool) {
    31  	if i := strings.IndexByte(s, sep); i >= 0 {
    32  		return s[:i], s[i+1:], true
    33  	}
    34  	return s, "", false
    35  }
    36  
    37  func Cut2(s, sep string) (before, after string, found bool) {
    38  	if i := strings.Index(s, sep); i >= 0 {
    39  		return s[:i], s[i+len(sep):], true
    40  	}
    41  	return s, "", false
    42  }
    43  
    44  func CutByte2(s string, sep byte) (before, after string, found bool) {
    45  	if i := strings.IndexByte(s, sep); i >= 0 {
    46  		return s[:i], s[i+1:], true
    47  	}
    48  	return s, "", false
    49  }
    50  
    51  func Cut3(s, sep string) (before, after string, found bool) {
    52  	if i := strings.Index(s, sep); i >= 0 {
    53  		return s[:i], s[i+len(sep):], true
    54  	}
    55  	return s, "", false
    56  }
    57  
    58  func CutByte3(s string, sep byte) (before, after string, found bool) {
    59  	if i := strings.IndexByte(s, sep); i >= 0 {
    60  		return s[:i], s[i+1:], true
    61  	}
    62  	return s, "", false
    63  }
    64  
    65  var before, after string
    66  var found bool
    67  
    68  func BenchmarkCut0(b *testing.B) {
    69  	b.ReportAllocs()
    70  
    71  	for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
    72  		s := strings.Repeat(strings.Repeat(" ", skip)+"a"+strings.Repeat(" ", skip), 1<<16/skip)
    73  		b.Run(fmt.Sprintf("func=Cut/skip=%d", skip), func(b *testing.B) {
    74  			for i := 0; i < b.N; i++ {
    75  				before, after, found = Cut0(s, "a")
    76  			}
    77  		})
    78  
    79  		b.Run(fmt.Sprintf("func=CutByte/skip=%d", skip), func(b *testing.B) {
    80  			for i := 0; i < b.N; i++ {
    81  				before, after, found = CutByte0(s, 'a')
    82  			}
    83  		})
    84  	}
    85  }
    86  func BenchmarkCut1(b *testing.B) {
    87  	b.ReportAllocs()
    88  
    89  	for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
    90  		s := strings.Repeat(strings.Repeat(" ", skip)+"a"+strings.Repeat(" ", skip), 1<<16/skip)
    91  		b.Run(fmt.Sprintf("func=Cut/skip=%d", skip), func(b *testing.B) {
    92  			for i := 0; i < b.N; i++ {
    93  				before, after, found = Cut1(s, "a")
    94  			}
    95  		})
    96  
    97  		b.Run(fmt.Sprintf("func=CutByte/skip=%d", skip), func(b *testing.B) {
    98  			for i := 0; i < b.N; i++ {
    99  				before, after, found = CutByte1(s, 'a')
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  func BenchmarkCut2(b *testing.B) {
   106  	b.ReportAllocs()
   107  
   108  	for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
   109  		s := strings.Repeat(strings.Repeat(" ", skip)+"a"+strings.Repeat(" ", skip), 1<<16/skip)
   110  		b.Run(fmt.Sprintf("func=Cut/skip=%d", skip), func(b *testing.B) {
   111  			for i := 0; i < b.N; i++ {
   112  				before, after, found = Cut2(s, "a")
   113  			}
   114  		})
   115  
   116  		b.Run(fmt.Sprintf("func=CutByte/skip=%d", skip), func(b *testing.B) {
   117  			for i := 0; i < b.N; i++ {
   118  				before, after, found = CutByte2(s, 'a')
   119  			}
   120  		})
   121  	}
   122  }
   123  
   124  func BenchmarkCut3(b *testing.B) {
   125  	b.ReportAllocs()
   126  
   127  	for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
   128  		s := strings.Repeat(strings.Repeat(" ", skip)+"a"+strings.Repeat(" ", skip), 1<<16/skip)
   129  		b.Run(fmt.Sprintf("func=Cut/skip=%d", skip), func(b *testing.B) {
   130  			for i := 0; i < b.N; i++ {
   131  				before, after, found = Cut3(s, "a")
   132  			}
   133  		})
   134  
   135  		b.Run(fmt.Sprintf("func=CutByte/skip=%d", skip), func(b *testing.B) {
   136  			for i := 0; i < b.N; i++ {
   137  				before, after, found = CutByte3(s, 'a')
   138  			}
   139  		})
   140  	}
   141  }