...
Source file
src/slices/sort_benchmark_test.go
Documentation: slices
1
2
3
4
5 package slices_test
6
7 import (
8 "fmt"
9 "slices"
10 "testing"
11 )
12
13 func BenchmarkBinarySearchFloats(b *testing.B) {
14 for _, size := range []int{16, 32, 64, 128, 512, 1024} {
15 b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
16 floats := make([]float64, size)
17 for i := range floats {
18 floats[i] = float64(i)
19 }
20 midpoint := len(floats) / 2
21 needle := (floats[midpoint] + floats[midpoint+1]) / 2
22 b.ResetTimer()
23 for i := 0; i < b.N; i++ {
24 slices.BinarySearch(floats, needle)
25 }
26 })
27 }
28 }
29
30 type myStruct struct {
31 a, b, c, d string
32 n int
33 }
34
35 func BenchmarkBinarySearchFuncStruct(b *testing.B) {
36 for _, size := range []int{16, 32, 64, 128, 512, 1024} {
37 b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
38 structs := make([]*myStruct, size)
39 for i := range structs {
40 structs[i] = &myStruct{n: i}
41 }
42 midpoint := len(structs) / 2
43 needle := &myStruct{n: (structs[midpoint].n + structs[midpoint+1].n) / 2}
44 lessFunc := func(a, b *myStruct) int { return a.n - b.n }
45 b.ResetTimer()
46 for i := 0; i < b.N; i++ {
47 slices.BinarySearchFunc(structs, needle, lessFunc)
48 }
49 })
50 }
51 }
52
View as plain text