-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbench_test.go
More file actions
55 lines (49 loc) · 1.17 KB
/
bench_test.go
File metadata and controls
55 lines (49 loc) · 1.17 KB
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
package base58_test
import (
"math/big"
"testing"
"github.com/tv42/base58"
"strconv"
)
func fixedBigInt(numBytes int) *big.Int {
data := make([]byte, numBytes)
data[0] = 0xff // without this, it's just an inefficient zero
num := new(big.Int)
num.SetBytes(data)
return num
}
// 8192-byte benchmarks mirror the ones in encoding/base64, and
// results should be comparable to those.
//
// 8-byte benchmarks are more like the uses that this library was
// meant for: smallish identifiers.
func BenchmarkEncode(b *testing.B) {
run := func(numBytes int) {
b.Run(strconv.Itoa(numBytes), func(b *testing.B) {
num := fixedBigInt(numBytes)
b.SetBytes(int64(len(num.Bytes())))
buf := make([]byte, 12000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = base58.EncodeBig(buf[:0], num)
}
})
}
run(8)
run(8192)
}
func BenchmarkBase58Decode(b *testing.B) {
run := func(numBytes int) {
b.Run(strconv.Itoa(numBytes), func(b *testing.B) {
num := fixedBigInt(numBytes)
encoded := base58.EncodeBig(nil, num)
b.SetBytes(int64(len(encoded)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = base58.DecodeToBig(encoded)
}
})
}
run(8)
run(8192)
}