Skip to content

Commit

Permalink
Merge pull request #505 from iand/master
Browse files Browse the repository at this point in the history
util: speed up failure path of ByteToBase10
  • Loading branch information
mreiferson committed Nov 24, 2014
2 parents 365d60c + eeeef12 commit f7da860
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions util/byte_base10.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
)

var base10fail = errors.New("failed to convert to Base10")

func ByteToBase10(b []byte) (n uint64, err error) {
base := uint64(10)

Expand All @@ -16,8 +18,8 @@ func ByteToBase10(b []byte) (n uint64, err error) {
v = d - '0'
default:
n = 0
err = errors.New("failed to convert to Base10")
break
err = base10fail
return
}
n *= base
n += uint64(v)
Expand Down
25 changes: 25 additions & 0 deletions util/byte_base10_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package util

import (
"testing"
)

var result uint64

func BenchmarkByteToBase10Valid(b *testing.B) {
bt := []byte{'3', '1', '4', '1', '5', '9', '2', '5'}
var n uint64
for i := 0; i < b.N; i++ {
n, _ = ByteToBase10(bt)
}
result = n
}

func BenchmarkByteToBase10Invalid(b *testing.B) {
bt := []byte{'?', '1', '4', '1', '5', '9', '2', '5'}
var n uint64
for i := 0; i < b.N; i++ {
n, _ = ByteToBase10(bt)
}
result = n
}

0 comments on commit f7da860

Please sign in to comment.