diff options
author | Kyle Consalus <consalus@gmail.com> | 2010-05-18 16:29:24 -0700 |
---|---|---|
committer | Kyle Consalus <consalus@gmail.com> | 2010-05-18 16:29:24 -0700 |
commit | 27716934e0e621817fe78cd01d4b05971bdcbdf6 (patch) | |
tree | 0145edbde47e4329fa102b3d3d791ffe178e2056 /src | |
parent | bb0a7fa32dd9b1a61b926d3ef67e99b43735f06c (diff) | |
download | golang-27716934e0e621817fe78cd01d4b05971bdcbdf6.tar.gz |
Trivial optimization.
Cached string indexing in inner loop of Btoui64.
Before:
strconv_test.BenchmarkAtoi 5000000 309 ns/op
strconv_test.BenchmarkAtoiNeg 5000000 325 ns/op
strconv_test.BenchmarkAtoi64 5000000 465 ns/op
strconv_test.BenchmarkAtoi64Neg 5000000 469 ns/op
After:
strconv_test.BenchmarkAtoi 10000000 182 ns/op
strconv_test.BenchmarkAtoiNeg 10000000 193 ns/op
strconv_test.BenchmarkAtoi64 10000000 251 ns/op
strconv_test.BenchmarkAtoi64Neg 10000000 258 ns/op
R=golang-dev, gri
CC=golang-dev
http://codereview.appspot.com/1227042
Committer: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/pkg/strconv/atoi.go | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/pkg/strconv/atoi.go b/src/pkg/strconv/atoi.go index 60492b653..e82b6cdba 100644 --- a/src/pkg/strconv/atoi.go +++ b/src/pkg/strconv/atoi.go @@ -77,13 +77,14 @@ func Btoui64(s string, b int) (n uint64, err os.Error) { for i := 0; i < len(s); i++ { var v byte + d := s[i] switch { - case '0' <= s[i] && s[i] <= '9': - v = s[i] - '0' - case 'a' <= s[i] && s[i] <= 'z': - v = s[i] - 'a' + 10 - case 'A' <= s[i] && s[i] <= 'Z': - v = s[i] - 'A' + 10 + case '0' <= d && d <= '9': + v = d - '0' + case 'a' <= d && d <= 'z': + v = d - 'a' + 10 + case 'A' <= d && d <= 'Z': + v = d - 'A' + 10 default: n = 0 err = os.EINVAL |