File: benchmarks_test.go

package info (click to toggle)
kitty 0.46.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,064 kB
  • sloc: ansic: 91,642; python: 59,657; objc: 6,273; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (108 lines) | stat: -rw-r--r-- 2,167 bytes parent folder | download
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
// License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>

package simdstring

import (
	"bytes"
	"fmt"
	"testing"
)

var _ = fmt.Print

func haystack(filler, needle byte, pos int) []byte {
	var data []byte
	if pos > 0 {
		data = append(bytes.Repeat([]byte{filler}, pos-1), needle)
	} else {
		data = []byte{needle}
	}
	return data
}

var sizes = []int{6, 327, 9875, 1198673}

func BenchmarkIndexByte(b *testing.B) {
	t := func(pos int, which string) {
		data := haystack('a', 'q', pos)
		f := IndexByte
		switch which {
		case "scalar":
			f = index_byte_scalar
		case "stdlib":
			f = bytes.IndexByte
		}
		b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
			for b.Loop() {
				f(data, 'q')
			}
		})
	}
	for _, pos := range sizes {
		t(pos, "simdstring")
		t(pos, "scalar")
		t(pos, "stdlib")
	}
}

func BenchmarkIndexByte2(b *testing.B) {
	t := func(pos int, which string) {
		data := haystack('a', 'q', pos)
		f := IndexByte2
		switch which {
		case "scalar":
			f = index_byte2_scalar
		}
		b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
			for b.Loop() {
				f(data, 'q', 'x')
			}
		})
	}
	for _, pos := range sizes {
		t(pos, "simdstring")
		t(pos, "scalar")
	}
}

func BenchmarkNotIndexByte(b *testing.B) {
	t := func(pos int, which string) {
		// Fill with 'a' and place 'q' (a non-matching byte) at the target position
		data := haystack('a', 'q', pos)
		f := NotIndexByte
		switch which {
		case "scalar":
			f = not_index_byte_scalar
		}
		b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
			for b.Loop() {
				f(data, 'a')
			}
		})
	}
	for _, pos := range sizes {
		t(pos, "simdstring")
		t(pos, "scalar")
	}
}

func BenchmarkNotIndexByte2(b *testing.B) {
	t := func(pos int, which string) {
		// Fill with 'a' and place 'q' (neither 'a' nor 'x') at the target position
		data := haystack('a', 'q', pos)
		f := NotIndexByte2
		switch which {
		case "scalar":
			f = not_index_byte2_scalar
		}
		b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
			for b.Loop() {
				f(data, 'a', 'x')
			}
		})
	}
	for _, pos := range sizes {
		t(pos, "simdstring")
		t(pos, "scalar")
	}
}