1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
package runewidth
import (
"testing"
"unicode/utf8"
)
var benchSink int
//
// RuneWidth
//
func benchRuneWidth(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int {
n := 0
got := -1
c := NewCondition()
c.EastAsianWidth = eastAsianWidth
for i := 0; i < b.N; i++ {
got = n
for r := start; r < stop; r++ {
n += c.RuneWidth(r)
}
got = n - got
}
if want != 0 && got != want { // some extra checks
b.Errorf("got %d, want %d\n", got, want)
}
return n
}
func BenchmarkRuneWidthAll(b *testing.B) {
benchSink = benchRuneWidth(b, false, 0, utf8.MaxRune+1, 1293932)
}
func BenchmarkRuneWidth768(b *testing.B) {
benchSink = benchRuneWidth(b, false, 0, 0x300, 702)
}
func BenchmarkRuneWidthAllEastAsian(b *testing.B) {
benchSink = benchRuneWidth(b, true, 0, utf8.MaxRune+1, 1432558)
}
func BenchmarkRuneWidth768EastAsian(b *testing.B) {
benchSink = benchRuneWidth(b, true, 0, 0x300, 794)
}
//
// String1Width - strings which consist of a single rune
//
func benchString1Width(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int {
n := 0
got := -1
c := NewCondition()
c.EastAsianWidth = eastAsianWidth
for i := 0; i < b.N; i++ {
got = n
for r := start; r < stop; r++ {
s := string(r)
n += c.StringWidth(s)
}
got = n - got
}
if want != 0 && got != want { // some extra checks
b.Errorf("got %d, want %d\n", got, want)
}
return n
}
func BenchmarkString1WidthAll(b *testing.B) {
benchSink = benchString1Width(b, false, 0, utf8.MaxRune+1, 1295980)
}
func BenchmarkString1Width768(b *testing.B) {
benchSink = benchString1Width(b, false, 0, 0x300, 702)
}
func BenchmarkString1WidthAllEastAsian(b *testing.B) {
benchSink = benchString1Width(b, true, 0, utf8.MaxRune+1, 1436654)
}
func BenchmarkString1Width768EastAsian(b *testing.B) {
benchSink = benchString1Width(b, true, 0, 0x300, 794)
}
//
// tables
//
func benchTable(b *testing.B, tbl table) int {
n := 0
for i := 0; i < b.N; i++ {
for r := rune(0); r <= utf8.MaxRune; r++ {
if inTable(r, tbl) {
n++
}
}
}
return n
}
func BenchmarkTablePrivate(b *testing.B) {
benchSink = benchTable(b, private)
}
func BenchmarkTableNonprint(b *testing.B) {
benchSink = benchTable(b, nonprint)
}
func BenchmarkTableCombining(b *testing.B) {
benchSink = benchTable(b, combining)
}
func BenchmarkTableDoublewidth(b *testing.B) {
benchSink = benchTable(b, doublewidth)
}
func BenchmarkTableAmbiguous(b *testing.B) {
benchSink = benchTable(b, ambiguous)
}
func BenchmarkTableEmoji(b *testing.B) {
benchSink = benchTable(b, emoji)
}
func BenchmarkTableNarrow(b *testing.B) {
benchSink = benchTable(b, narrow)
}
func BenchmarkTableNeutral(b *testing.B) {
benchSink = benchTable(b, neutral)
}
|