-
Notifications
You must be signed in to change notification settings - Fork 4
/
int_bench_test.go
62 lines (59 loc) · 1.1 KB
/
int_bench_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
package jx
import (
"bytes"
"encoding/json"
"strconv"
"testing"
)
func BenchmarkEncoder_Int(b *testing.B) {
const v = 0xffffff
b.Run("Strconv", func(b *testing.B) {
b.ReportAllocs()
var buf []byte
for i := 0; i < b.N; i++ {
buf = buf[:0]
buf = strconv.AppendInt(buf, v, 10)
}
})
b.Run("Std", func(b *testing.B) {
b.ReportAllocs()
buf := new(bytes.Buffer)
e := json.NewEncoder(buf)
for i := 0; i < b.N; i++ {
buf.Reset()
if err := e.Encode(v); err != nil {
b.Fatal(err)
}
}
})
b.Run("JX", func(b *testing.B) {
b.ReportAllocs()
e := GetEncoder()
for i := 0; i < b.N; i++ {
e.Reset()
e.UInt64(v)
}
})
}
func BenchmarkDecoder_Int64(b *testing.B) {
input := []byte(`100`)
b.Run("Std", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result := int64(0)
if err := json.Unmarshal(input, &result); err != nil {
b.Fatal(err)
}
}
})
b.Run("JX", func(b *testing.B) {
b.ReportAllocs()
d := GetDecoder()
for i := 0; i < b.N; i++ {
d.ResetBytes(input)
if _, err := d.Int64(); err != nil {
b.Fatal(err)
}
}
})
}