...

Source file src/github.com/mattn/go-runewidth/benchmark_test.go

Documentation: github.com/mattn/go-runewidth

     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  //
   129  // tables
   130  //
   131  func benchTable(b *testing.B, tbl table) int {
   132  	n := 0
   133  	for i := 0; i < b.N; i++ {
   134  		for r := rune(0); r <= utf8.MaxRune; r++ {
   135  			if inTable(r, tbl) {
   136  				n++
   137  			}
   138  		}
   139  	}
   140  	return n
   141  }
   142  
   143  func BenchmarkTablePrivate(b *testing.B) {
   144  	benchSink = benchTable(b, private)
   145  }
   146  func BenchmarkTableNonprint(b *testing.B) {
   147  	benchSink = benchTable(b, nonprint)
   148  }
   149  func BenchmarkTableCombining(b *testing.B) {
   150  	benchSink = benchTable(b, combining)
   151  }
   152  func BenchmarkTableDoublewidth(b *testing.B) {
   153  	benchSink = benchTable(b, doublewidth)
   154  }
   155  func BenchmarkTableAmbiguous(b *testing.B) {
   156  	benchSink = benchTable(b, ambiguous)
   157  }
   158  func BenchmarkTableEmoji(b *testing.B) {
   159  	benchSink = benchTable(b, emoji)
   160  }
   161  func BenchmarkTableNarrow(b *testing.B) {
   162  	benchSink = benchTable(b, narrow)
   163  }
   164  func BenchmarkTableNeutral(b *testing.B) {
   165  	benchSink = benchTable(b, neutral)
   166  }
   167  

View as plain text