File: uniseg_bench_test.go

package info (click to toggle)
golang-sourcehut-rockorager-vaxis 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: makefile: 9
file content (73 lines) | stat: -rw-r--r-- 1,537 bytes parent folder | download | duplicates (2)
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
package vaxis_test

import (
	"strings"
	"testing"

	"github.com/rivo/uniseg"
)

func BenchmarkUniseg(b *testing.B) {
	const testString = "๐Ÿ˜€๐Ÿ”ฎ๐ŸŒ๐Ÿ“test string"

	b.Run("rune reader", func(b *testing.B) {
		// Just so we can see the penalty
		for i := 0; i < b.N; i += 1 {
			result := []string{}
			r := strings.NewReader(testString)
			for {
				ch, _, err := r.ReadRune()
				if err != nil {
					break
				}
				result = append(result, string(ch))
			}
		}
	})

	b.Run("graphemes", func(b *testing.B) {
		for i := 0; i < b.N; i += 1 {
			result := []string{}
			g := uniseg.NewGraphemes(testString)
			for g.Next() {
				result = append(result, g.Str())
			}
		}
	})
	b.Run("step", func(b *testing.B) {
		for i := 0; i < b.N; i += 1 {
			result := [][]byte{}
			in := []byte(testString)
			state := -1
			cluster := []byte{}
			for len(in) > 0 {
				cluster, in, _, state = uniseg.Step(in, state)
				result = append(result, cluster)
			}
		}
	})
	b.Run("stepstring", func(b *testing.B) {
		for i := 0; i < b.N; i += 1 {
			result := []string{}
			in := testString
			state := -1
			cluster := ""
			for len(in) > 0 {
				cluster, in, _, state = uniseg.StepString(in, state)
				result = append(result, cluster)
			}
		}
	})
	b.Run("firstfunction", func(b *testing.B) {
		for i := 0; i < b.N; i += 1 {
			result := []string{}
			in := testString
			state := -1
			cluster := ""
			for len(in) > 0 {
				cluster, in, _, state = uniseg.FirstGraphemeClusterInString(in, state)
				result = append(result, cluster)
			}
		}
	})
}