| 1 |
// Copyright 2023 Charlie Vieth. All rights reserved. |
| 2 |
// Use of this source code is governed by the MIT license. |
| 3 |
|
| 4 |
package strcase |
| 5 |
|
| 6 |
import ( |
| 7 |
"flag" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
var calibrate = flag.Bool("calibrate", false, "compute crossover for linear vs. binary search") |
| 13 |
|
| 14 |
// // TODO: use this |
| 15 |
// // |
| 16 |
// // TestCalibrate determines the cutoff where a brute-force-search is faster |
| 17 |
// // than the current Index algorithm. |
| 18 |
// func TestCalibrateBruteForce(t *testing.T) { |
| 19 |
// if !*calibrate { |
| 20 |
// return |
| 21 |
// } |
| 22 |
// |
| 23 |
// // TODO: run this on amd64 |
| 24 |
// if runtime.GOARCH == "amd64" { |
| 25 |
// fmt.Printf("warning: running calibration on %s\n", runtime.GOARCH) |
| 26 |
// } |
| 27 |
// |
| 28 |
// bench := func(t *testing.T, name, prefix string) { |
| 29 |
// t.Run(name, func(t *testing.T) { |
| 30 |
// n := sort.Search(64, func(n int) bool { |
| 31 |
// key := prefix + "a" |
| 32 |
// s := strings.Repeat(prefix+strings.Repeat(" ", n-1), 1<<16/n) |
| 33 |
// bruteForce := func(b *testing.B) { |
| 34 |
// for i := 0; i < b.N; i++ { |
| 35 |
// bruteForceIndexUnicode(s, key) |
| 36 |
// } |
| 37 |
// } |
| 38 |
// shortIndex := func(b *testing.B) { |
| 39 |
// for i := 0; i < b.N; i++ { |
| 40 |
// shortIndexUnicode(s, key) |
| 41 |
// } |
| 42 |
// } |
| 43 |
// bmbrute := testing.Benchmark(bruteForce) |
| 44 |
// bmshort := testing.Benchmark(shortIndex) |
| 45 |
// fmt.Printf("n=%d: brute=%d index=%d\n", n, bmbrute.NsPerOp(), bmshort.NsPerOp()) |
| 46 |
// return bmbrute.NsPerOp()*100 > bmshort.NsPerOp()*110 |
| 47 |
// }) |
| 48 |
// fmt.Printf("calibration: brute-force cutoff = %d\n", n) |
| 49 |
// }) |
| 50 |
// } |
| 51 |
// |
| 52 |
// bench(t, "Unicode", "α") |
| 53 |
// bench(t, "ASCII", "a") |
| 54 |
// } |
| 55 |
|
| 56 |
// This test checks if the performance of strings.LastIndexByte has improved |
| 57 |
// and beats the naive implementation (based on Go 1.19) that is used here. |
| 58 |
func TestCalibrateLastIndexByte(t *testing.T) { |
| 59 |
if !*calibrate { |
| 60 |
return |
| 61 |
} |
| 62 |
|
| 63 |
s := "1" + strings.Repeat("a", 256) |
| 64 |
c := byte('1') |
| 65 |
|
| 66 |
benchStringsLastIndexByte := func(b *testing.B) { |
| 67 |
for i := 0; i < b.N; i++ { |
| 68 |
strings.LastIndexByte(s, c) |
| 69 |
} |
| 70 |
} |
| 71 |
benchLastIndexByte := func(b *testing.B) { |
| 72 |
for i := 0; i < b.N; i++ { |
| 73 |
LastIndexByte(s, c) |
| 74 |
} |
| 75 |
} |
| 76 |
nsStrings := testing.Benchmark(benchStringsLastIndexByte).NsPerOp() |
| 77 |
nsStrcase := testing.Benchmark(benchLastIndexByte).NsPerOp() |
| 78 |
|
| 79 |
t.Logf("strings=%d stracse=%d\n", nsStrings, nsStrcase) |
| 80 |
|
| 81 |
// If strings.LastIndexByte is 1.5x faster than the current naive |
| 82 |
// Go 1.19 implementation then we should take advantage of that. |
| 83 |
if nsStrings > nsStrcase+nsStrcase/2 { |
| 84 |
t.Fatalf("strings.LastIndexByte = %d ns/op LastIndexByte = %d ns/op Delta = %.2fx", |
| 85 |
nsStrings, nsStrcase, float64(nsStrcase)/float64(nsStrings)) |
| 86 |
} |
| 87 |
} |