From 7c6542dfbb41505111dde1f321eb5d5002f5fb88 Mon Sep 17 00:00:00 2001 From: Martin Lindhe Date: Fri, 19 Apr 2019 01:00:09 +0200 Subject: [PATCH] add BenchmarkEncodeBytesAsBytes --- Makefile | 2 +- base36.go | 13 ++++++------- base36_test.go | 9 +++++++++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 7c276cf..abf174a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ bench: - go test -bench=. + go test -benchmem -bench=. test: go test -v diff --git a/base36.go b/base36.go index 9d3cad1..2158458 100644 --- a/base36.go +++ b/base36.go @@ -31,7 +31,7 @@ var ( } ) -// Encode encodes a number to base36 +// Encode encodes a number to base36. func Encode(value uint64) string { var res [16]byte var i int @@ -42,14 +42,13 @@ func Encode(value uint64) string { return string(res[i+1:]) } -// Decode decodes a base36-encoded string +// Decode decodes a base36-encoded string. func Decode(s string) uint64 { res := uint64(0) l := len(s) - 1 for idx := range s { c := s[l-idx] - byteOffset := index[c] - res += uint64(byteOffset) * uint64(math.Pow(36, float64(idx))) + res += uint64(index[c]) * uint64(math.Pow(36, float64(idx))) } return res } @@ -57,7 +56,7 @@ func Decode(s string) uint64 { var bigRadix = big.NewInt(36) var bigZero = big.NewInt(0) -// EncodeBytesAsBytes encodes a byte slice to base36 +// EncodeBytesAsBytes encodes a byte slice to base36. func EncodeBytesAsBytes(b []byte) []byte { x := new(big.Int) x.SetBytes(b) @@ -86,12 +85,12 @@ func EncodeBytesAsBytes(b []byte) []byte { return answer } -// EncodeBytes encodes a byte slice to base36 string +// EncodeBytes encodes a byte slice to base36 string. func EncodeBytes(b []byte) string { return string(EncodeBytesAsBytes(b)) } -// DecodeToBytes decodes a modified base58 string to a byte slice, using alphabet. +// DecodeToBytes decodes a base36 string to a byte slice, using alphabet. func DecodeToBytes(b string) []byte { alphabet := string(base36) answer := big.NewInt(0) diff --git a/base36_test.go b/base36_test.go index 1ec140b..6a6b2a4 100644 --- a/base36_test.go +++ b/base36_test.go @@ -35,6 +35,15 @@ func BenchmarkEncode(b *testing.B) { } } +func BenchmarkEncodeBytesAsBytes(b *testing.B) { + data := []byte{ + 0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B, 0x48, 0x4F, 0x2A, 0x48, 0x4F, 0x2A, + } + for i := 0; i < b.N; i++ { + EncodeBytesAsBytes(data) + } +} + func BenchmarkDecode(b *testing.B) { for i := 0; i < b.N; i++ { Decode("1Y2P0IJ32E8E7")