-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_test.go
96 lines (83 loc) · 1.56 KB
/
algo_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
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
package main
import (
"crypto/rand"
"hash/adler32"
"hash/crc32"
"hash/crc64"
"hash/fnv"
"testing"
)
const size = 16 * 1024
var (
crc32k = crc32.MakeTable(crc32.Koopman)
crc64iso = crc64.MakeTable(crc64.ISO)
crc64ecma = crc64.MakeTable(crc64.ECMA)
)
func randomBuf(b *testing.B) []byte {
buf := make([]byte, size)
n, err := rand.Read(buf)
if n != size || err != nil {
b.Fatalf("failed to generate random data: %v", err)
}
return buf
}
func BenchmarkCRC32C(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
crc32.Checksum(buf, crc32c)
}
}
func BenchmarkCRC32K(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
crc32.Checksum(buf, crc32k)
}
}
func BenchmarkCRC32IEEE(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
crc32.ChecksumIEEE(buf)
}
}
func BenchmarkAdler32(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
adler32.Checksum(buf)
}
}
func BenchmarkCRC64ISO(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
crc64.Checksum(buf, crc64iso)
}
}
func BenchmarkCRC64ECMA(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
crc64.Checksum(buf, crc64ecma)
}
}
func BenchmarkFNV32(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := fnv.New32()
h.Write(buf)
h.Sum(nil)
}
}
func BenchmarkFNV64(b *testing.B) {
buf := randomBuf(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := fnv.New64a()
h.Write(buf)
h.Sum(nil)
}
}