gitee.com/quant1x/gox@v1.21.2/text/runewidth/benchmark_test.go (about)

     1  package runewidth
     2  
     3  import (
     4  	"testing"
     5  	"unicode/utf8"
     6  )
     7  
     8  var benchSink int
     9  
    10  //
    11  // RuneWidth
    12  //
    13  
    14  func benchRuneWidth(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int {
    15  	b.Helper()
    16  	n := 0
    17  	b.Run("regular", func(b *testing.B) {
    18  		got := -1
    19  		c := NewCondition()
    20  		c.EastAsianWidth = eastAsianWidth
    21  		b.ReportAllocs()
    22  		b.ResetTimer()
    23  		for i := 0; i < b.N; i++ {
    24  			got = n
    25  			for r := start; r < stop; r++ {
    26  				n += c.RuneWidth(r)
    27  			}
    28  			got = n - got
    29  		}
    30  		if want != 0 && got != want { // some extra checks
    31  			b.Errorf("got %d, want %d\n", got, want)
    32  		}
    33  	})
    34  	b.Run("lut", func(b *testing.B) {
    35  		got := -1
    36  		n = 0
    37  		c := NewCondition()
    38  		c.EastAsianWidth = eastAsianWidth
    39  		c.CreateLUT()
    40  		b.ReportAllocs()
    41  		b.ResetTimer()
    42  		for i := 0; i < b.N; i++ {
    43  			got = n
    44  			for r := start; r < stop; r++ {
    45  				n += c.RuneWidth(r)
    46  			}
    47  			got = n - got
    48  		}
    49  		if want != 0 && got != want { // some extra checks
    50  			b.Errorf("got %d, want %d\n", got, want)
    51  		}
    52  	})
    53  	return n
    54  }
    55  func BenchmarkRuneWidthAll(b *testing.B) {
    56  	benchSink = benchRuneWidth(b, false, 0, utf8.MaxRune+1, 1293942)
    57  }
    58  func BenchmarkRuneWidth768(b *testing.B) {
    59  	benchSink = benchRuneWidth(b, false, 0, 0x300, 702)
    60  }
    61  func BenchmarkRuneWidthAllEastAsian(b *testing.B) {
    62  	benchSink = benchRuneWidth(b, true, 0, utf8.MaxRune+1, 1432568)
    63  }
    64  func BenchmarkRuneWidth768EastAsian(b *testing.B) {
    65  	benchSink = benchRuneWidth(b, true, 0, 0x300, 794)
    66  }
    67  
    68  //
    69  // String1Width - strings which consist of a single rune
    70  //
    71  
    72  func benchString1Width(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int {
    73  	b.Helper()
    74  	n := 0
    75  	b.Run("regular", func(b *testing.B) {
    76  		got := -1
    77  		c := NewCondition()
    78  		c.EastAsianWidth = eastAsianWidth
    79  		b.ResetTimer()
    80  		b.ReportAllocs()
    81  		for i := 0; i < b.N; i++ {
    82  			got = n
    83  			for r := start; r < stop; r++ {
    84  				s := string(r)
    85  				n += c.StringWidth(s)
    86  			}
    87  			got = n - got
    88  		}
    89  		if want != 0 && got != want { // some extra checks
    90  			b.Errorf("got %d, want %d\n", got, want)
    91  		}
    92  	})
    93  	b.Run("lut", func(b *testing.B) {
    94  		got := -1
    95  		n = 0
    96  		c := NewCondition()
    97  		c.EastAsianWidth = eastAsianWidth
    98  		c.CreateLUT()
    99  		b.ResetTimer()
   100  		b.ReportAllocs()
   101  		for i := 0; i < b.N; i++ {
   102  			got = n
   103  			for r := start; r < stop; r++ {
   104  				s := string(r)
   105  				n += c.StringWidth(s)
   106  			}
   107  			got = n - got
   108  		}
   109  		if want != 0 && got != want { // some extra checks
   110  			b.Errorf("got %d, want %d\n", got, want)
   111  		}
   112  	})
   113  	return n
   114  }
   115  func BenchmarkString1WidthAll(b *testing.B) {
   116  	benchSink = benchString1Width(b, false, 0, utf8.MaxRune+1, 1295990)
   117  }
   118  func BenchmarkString1Width768(b *testing.B) {
   119  	benchSink = benchString1Width(b, false, 0, 0x300, 702)
   120  }
   121  func BenchmarkString1WidthAllEastAsian(b *testing.B) {
   122  	benchSink = benchString1Width(b, true, 0, utf8.MaxRune+1, 1436664)
   123  }
   124  func BenchmarkString1Width768EastAsian(b *testing.B) {
   125  	benchSink = benchString1Width(b, true, 0, 0x300, 794)
   126  }
   127  
   128  // tables
   129  func benchTable(b *testing.B, tbl table) int {
   130  	n := 0
   131  	for i := 0; i < b.N; i++ {
   132  		for r := rune(0); r <= utf8.MaxRune; r++ {
   133  			if inTable(r, tbl) {
   134  				n++
   135  			}
   136  		}
   137  	}
   138  	return n
   139  }
   140  
   141  func BenchmarkTablePrivate(b *testing.B) {
   142  	benchSink = benchTable(b, private)
   143  }
   144  func BenchmarkTableNonprint(b *testing.B) {
   145  	benchSink = benchTable(b, nonprint)
   146  }
   147  func BenchmarkTableCombining(b *testing.B) {
   148  	benchSink = benchTable(b, combining)
   149  }
   150  func BenchmarkTableDoublewidth(b *testing.B) {
   151  	benchSink = benchTable(b, doublewidth)
   152  }
   153  func BenchmarkTableAmbiguous(b *testing.B) {
   154  	benchSink = benchTable(b, ambiguous)
   155  }
   156  func BenchmarkTableEmoji(b *testing.B) {
   157  	benchSink = benchTable(b, emoji)
   158  }
   159  func BenchmarkTableNarrow(b *testing.B) {
   160  	benchSink = benchTable(b, narrow)
   161  }
   162  func BenchmarkTableNeutral(b *testing.B) {
   163  	benchSink = benchTable(b, neutral)
   164  }