diff options
author | Marvin Stenger <marvin.stenger94@gmail.com> | 2018-05-03 12:28:43 +0200 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-05-03 18:07:02 +0000 |
commit | 3c0bf181b79406a71b7b2fd22b4d222722b96649 (patch) | |
tree | 177925e749b47a01b8915d66ae184be564111d8c /src/strconv | |
parent | 63756e0c8f76856a436274fc47d912815f9f17e6 (diff) | |
download | go-3c0bf181b79406a71b7b2fd22b4d222722b96649.tar.gz go-3c0bf181b79406a71b7b2fd22b4d222722b96649.zip |
strconv: simplify and optimize Itoa(small)
Use substring of digits for values < 10.
name old time/op new time/op delta
FormatIntSmall/7-4 4.54ns ± 1% 3.70ns ± 1% -18.41% (p=0.000 n=18+17)
FormatIntSmall/42-4 4.54ns ± 1% 4.13ns ± 1% -9.02% (p=0.000 n=16+18)
Change-Id: I0b521b563c13ef88aa2701049fa4a43760e884af
Reviewed-on: https://go-review.googlesource.com/111285
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/strconv')
-rw-r--r-- | src/strconv/itoa.go | 5 | ||||
-rw-r--r-- | src/strconv/itoa_test.go | 12 |
2 files changed, 10 insertions, 7 deletions
diff --git a/src/strconv/itoa.go b/src/strconv/itoa.go index 394716ccd7..8afe7af251 100644 --- a/src/strconv/itoa.go +++ b/src/strconv/itoa.go @@ -57,11 +57,10 @@ func AppendUint(dst []byte, i uint64, base int) []byte { // small returns the string for an i with 0 <= i < nSmalls. func small(i int) string { - off := 0 if i < 10 { - off = 1 + return digits[i : i+1] } - return smallsString[i*2+off : i*2+2] + return smallsString[i*2 : i*2+2] } const nSmalls = 100 diff --git a/src/strconv/itoa_test.go b/src/strconv/itoa_test.go index 89c2de6941..b5ee3aa828 100644 --- a/src/strconv/itoa_test.go +++ b/src/strconv/itoa_test.go @@ -200,10 +200,14 @@ func BenchmarkAppendUint(b *testing.B) { } func BenchmarkFormatIntSmall(b *testing.B) { - const smallInt = 42 - for i := 0; i < b.N; i++ { - s := FormatInt(smallInt, 10) - BenchSink += len(s) + smallInts := []int64{7, 42} + for _, smallInt := range smallInts { + b.Run(Itoa(int(smallInt)), func(b *testing.B) { + for i := 0; i < b.N; i++ { + s := FormatInt(smallInt, 10) + BenchSink += len(s) + } + }) } } |