https://earth.google.com/web/@29.996822,-90.136449,0a,1686d,30y,270h,30t,0r/data=KAI?utm_source=referral&utm_campaign=earthview&utm_term=extension

优化GO语言sort.Slice排序

优化GO语言sort.Slice排序 介绍 众所周知,go对复杂Slice排序有两种方法,分别是: sort.Slice(x any, cmp func(i, j int)) 实现sort.Interface接口,调用sort.Sort()进行排序 今天在写Gazelle时候,写到了一个需要Slice排序的场景。突发奇想,将上列两种方法对比,哪一种速度更快。Ps. Benchmark写法和go test -bench命令自行百度 开始 新建一个sort_test.go 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 package sort import ( "sort" "testing" ) type testSlice []struct { order int } func (t testSlice) Len() int { return len(t) } func (t testSlice) Less(i, j int) bool { // > 是倒序 // < 是升序 return t[i]....

2023-09-22 14:03:25+08:00 · 3 分钟 · 563 字 · lazychanger