-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemset_test.go
65 lines (58 loc) · 1.07 KB
/
memset_test.go
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
package and
import (
"math/rand/v2"
"testing"
)
func memsetNaive(dst []byte, b byte) {
for i := range dst {
dst[i] = b
}
}
func testMemset(t *testing.T, size int) {
a := make([]byte, size)
Memset(a, 0xff)
for i, v := range a {
if v != 0xff {
t.Errorf("Memset failed to set a[%d] to 0xff", i)
}
}
}
func TestMemsetAgainstGeneric(t *testing.T) {
for i := 0; i < 20; i++ {
size := 1 << i
testMemset(t, size)
for j := 0; j < 10; j++ {
testMemset(t, size+rand.IntN(100))
}
}
}
func BenchmarkMemset(b *testing.B) {
b.StopTimer()
size := 1000000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
Memset(a, 0xff)
}
}
func BenchmarkMemsetGeneric(b *testing.B) {
b.StopTimer()
size := 1000000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
memsetGeneric(a, 0xff)
}
}
func BenchmarkMemsetNaive(b *testing.B) {
b.StopTimer()
size := 1000000
a := make([]byte, size)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
memsetNaive(a, 0xff)
}
}