Skip to content

Commit

Permalink
add BenchmarkEncodeBytesAsBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlindhe committed Apr 18, 2019
1 parent 069ed0a commit 7c6542d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bench:
go test -bench=.
go test -benchmem -bench=.

test:
go test -v
13 changes: 6 additions & 7 deletions base36.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,22 +42,21 @@ 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
}

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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions base36_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 7c6542d

Please sign in to comment.