]> Cypherpunks.ru repositories - gostls13.git/commitdiff
all: faster midpoint computation in binary search
authoryangwenmai <yangwen.yw@gmail.com>
Fri, 25 Dec 2020 01:36:41 +0000 (09:36 +0800)
committerRobert Griesemer <gri@golang.org>
Tue, 23 Feb 2021 01:37:31 +0000 (01:37 +0000)
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>

src/go/token/position.go
src/go/token/position_bench_test.go [new file with mode: 0644]
src/reflect/type.go
src/strconv/makeisprint.go
src/strconv/quote.go

index a21f5fd056027641a91a0c8cd8519beeab8057f1..bbcd8b022b677f211ed28c997bd976e3ac61d8b6 100644 (file)
@@ -540,7 +540,7 @@ func searchInts(a []int, x int) int {
        // TODO(gri): Remove this when compilers have caught up.
        i, j := 0, len(a)
        for i < j {
-               h := i + (j-i)/2 // avoid overflow when computing h
+               h := i + (j-i)>>1 // avoid overflow when computing h
                // i ≤ h < j
                if a[h] <= x {
                        i = h + 1
diff --git a/src/go/token/position_bench_test.go b/src/go/token/position_bench_test.go
new file mode 100644 (file)
index 0000000..41be728
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package token
+
+import (
+       "testing"
+)
+
+func BenchmarkSearchInts(b *testing.B) {
+       data := make([]int, 10000)
+       for i := 0; i < 10000; i++ {
+               data[i] = i
+       }
+       const x = 8
+       if r := searchInts(data, x); r != x {
+               b.Errorf("got index = %d; want %d", r, x)
+       }
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               searchInts(data, x)
+       }
+}
index d52816628f0d1e735929dc9d31c2b5e763b41aa7..eb2030063a38f87d2c7488f2e4c27f3ce0ac6a56 100644 (file)
@@ -1723,7 +1723,7 @@ func typesByString(s string) []*rtype {
                // This is a copy of sort.Search, with f(h) replaced by (*typ[h].String() >= s).
                i, j := 0, len(offs)
                for i < j {
-                       h := i + (j-i)/2 // avoid overflow when computing h
+                       h := i + (j-i)>>1 // avoid overflow when computing h
                        // i ≤ h < j
                        if !(rtypeOff(section, offs[h]).String() >= s) {
                                i = h + 1 // preserves f(i-1) == false
index 79678341d4639b0837e9b98ba1bba21051ddd658..909f9e47878b58f17362aea6f6646758e9f974fe 100644 (file)
@@ -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 {
index 4ffa10b72efc9d163640401c76092f47a16c125b..db0dbb288b4c9f5d9a6ab17433921d18a5e298ea 100644 (file)
@@ -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 {