1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 00:02:28 +08:00

perf(slice): make the IndexOf function thread-safe (#263)

This commit is contained in:
残念
2024-11-06 10:04:44 +08:00
committed by GitHub
parent 8bbae69175
commit a7fecfc73b
3 changed files with 64 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"testing"
"github.com/duke-git/lancet/v2/internal"
@@ -1367,6 +1368,37 @@ func TestIndexOf(t *testing.T) {
assert.Equal(-1, IndexOf(arr3, "r"))
assert.Equal(2, memoryHashCounter[key3])
assert.Equal(0, memoryHashCounter[minKey])
arr4 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
const numGoroutines = 100
var wg sync.WaitGroup
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(i int) {
defer wg.Done()
index := IndexOf(arr4, i%10+1)
assert.Equal(i%10, index)
}(i)
}
wg.Wait()
}
func BenchmarkIndexOfDifferentSizes(b *testing.B) {
sizes := []int{10, 100, 1000, 10000, 100000}
for _, size := range sizes {
arr := make([]int, size)
for i := 0; i < len(arr); i++ {
arr[i] = i
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = IndexOf(arr, size/2) // 查找数组中间的元素
}
})
}
}
func TestLastIndexOf(t *testing.T) {