-
Notifications
You must be signed in to change notification settings - Fork 6
/
io_test.go
106 lines (95 loc) · 2.55 KB
/
io_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
97
98
99
100
101
102
103
104
105
106
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package unitype
import (
"encoding/binary"
"io"
"os"
"testing"
)
/*
Comparing use of binary.Read vs binary.BigEndian.... direct use.
Benchmark results indicate that the performance in all cases is pretty comparable,
within 5% difference. Thus choosing the simplest option (first one).
BenchmarkBinaryRead-8 3000 509553 ns/op
--- BENCH: BenchmarkBinaryRead-8
io_test.go:26: Result: 1633005 (N: 1)
io_test.go:26: Result: 163300500 (N: 100)
io_test.go:26: Result: 4899015000 (N: 3000)
BenchmarkBinaryRead2-8 3000 492651 ns/op
--- BENCH: BenchmarkBinaryRead2-8
io_test.go:49: Result: 1633005 (N: 1)
io_test.go:49: Result: 163300500 (N: 100)
io_test.go:49: Result: 4899015000 (N: 3000)
BenchmarkBinaryRead3-8 3000 508578 ns/op
--- BENCH: BenchmarkBinaryRead3-8
io_test.go:76: Result: 1633005 (N: 1)
io_test.go:76: Result: 163300500 (N: 100)
io_test.go:76: Result: 4899015000 (N: 3000)
PASS
ok github.com/unidoc/unitype 10.357s
*/
func BenchmarkBinaryRead(b *testing.B) {
f, err := os.Open("./testdata/FreeSans.ttf")
if err != nil {
b.Fatalf("Error: %v", err)
}
defer f.Close()
var sum int64
for i := 0; i < b.N; i++ {
f.Seek(0, io.SeekStart)
for j := 0; j < 100; j++ {
var val offset16
binary.Read(f, binary.BigEndian, &val)
sum += int64(val)
}
}
b.Logf("Result: %d (N: %d)", sum, b.N)
}
func BenchmarkBinaryRead2(b *testing.B) {
f, err := os.Open("./testdata/FreeSans.ttf")
if err != nil {
b.Fatalf("Error: %v", err)
}
defer f.Close()
var sum int64
for i := 0; i < b.N; i++ {
f.Seek(0, io.SeekStart)
for j := 0; j < 100; j++ {
data := make([]byte, 2)
_, err = io.ReadFull(f, data)
if err != nil {
b.Fatalf("Error: %v", err)
}
val := offset16(binary.BigEndian.Uint16(data))
sum += int64(val)
}
}
b.Logf("Result: %d (N: %d)", sum, b.N)
}
func BenchmarkBinaryRead3(b *testing.B) {
f, err := os.Open("./testdata/FreeSans.ttf")
if err != nil {
b.Fatalf("Error: %v", err)
}
defer f.Close()
readOffset16 := func(r io.Reader) (offset16, error) {
var val offset16
err := binary.Read(f, binary.BigEndian, &val)
return val, err
}
var sum int64
for i := 0; i < b.N; i++ {
f.Seek(0, io.SeekStart)
for j := 0; j < 100; j++ {
val, err := readOffset16(f)
if err != nil {
b.Fatalf("Error: %v", err)
}
sum += int64(val)
}
}
b.Logf("Result: %d (N: %d)", sum, b.N)
}