diff options
author | yangwenmai <yangwen.yw@gmail.com> | 2020-12-25 09:36:41 +0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-02-23 01:37:31 +0000 |
commit | 7af821a661be57cdd13212695cd6c1095487f2b4 (patch) | |
tree | c1f1aa44f727848c6c4bb3ed3bd25379b325966c /src/strconv | |
parent | f113e9a14f08d23be78f75050185f9796a1d243f (diff) | |
download | go-7af821a661be57cdd13212695cd6c1095487f2b4.tar.gz go-7af821a661be57cdd13212695cd6c1095487f2b4.zip |
all: faster midpoint computation in binary search
On my machine (3.1 GHz Quad-Core Intel Core i7, macOS 10.15.7 10.15.7), go 1.15.6
benchstat:
name old time/op new time/op delta
SearchInts-8 20.3ns ± 1% 16.6ns ± 6% -18.37% (p=0.000 n=9+10)
Change-Id: I346e5955fd6df6ce10254b22267dbc8d5a2b16c0
Reviewed-on: https://go-review.googlesource.com/c/go/+/279439
Reviewed-by: Ben Shi <powerman1st@163.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/strconv')
-rw-r--r-- | src/strconv/makeisprint.go | 4 | ||||
-rw-r--r-- | src/strconv/quote.go | 4 |
2 files changed, 4 insertions, 4 deletions
diff --git a/src/strconv/makeisprint.go b/src/strconv/makeisprint.go index 79678341d4..909f9e4787 100644 --- a/src/strconv/makeisprint.go +++ b/src/strconv/makeisprint.go @@ -37,7 +37,7 @@ var ( func bsearch16(a []uint16, x uint16) int { i, j := 0, len(a) for i < j { - h := i + (j-i)/2 + h := i + (j-i)>>1 if a[h] < x { i = h + 1 } else { @@ -52,7 +52,7 @@ func bsearch16(a []uint16, x uint16) int { func bsearch32(a []uint32, x uint32) int { i, j := 0, len(a) for i < j { - h := i + (j-i)/2 + h := i + (j-i)>>1 if a[h] < x { i = h + 1 } else { diff --git a/src/strconv/quote.go b/src/strconv/quote.go index 4ffa10b72e..db0dbb288b 100644 --- a/src/strconv/quote.go +++ b/src/strconv/quote.go @@ -440,7 +440,7 @@ func Unquote(s string) (string, error) { func bsearch16(a []uint16, x uint16) int { i, j := 0, len(a) for i < j { - h := i + (j-i)/2 + h := i + (j-i)>>1 if a[h] < x { i = h + 1 } else { @@ -455,7 +455,7 @@ func bsearch16(a []uint16, x uint16) int { func bsearch32(a []uint32, x uint32) int { i, j := 0, len(a) for i < j { - h := i + (j-i)/2 + h := i + (j-i)>>1 if a[h] < x { i = h + 1 } else { |