Zxilly
Calm down
Zxilly's Blog

slices.Sort 和 sort.Sort 的简单 benchmark

测试代码

package gotmp

import (
    "math/rand"
    "slices"
    "sort"
    "testing"
)

type IntSlice []int

func (s IntSlice) Len() int           { return len(s) }
func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
func (s IntSlice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }

func generateRandomSlice(n int) []int {
    slice := make([]int, n)
    for i := range slice {
        slice[i] = rand.Int()
    }
    return slice
}

func BenchmarkSlicesSort(b *testing.B) {
    for i := 0; i < b.N; i++ {
        b.StopTimer()
        slice := generateRandomSlice(1000)
        b.StartTimer()
        slices.Sort(slice)
    }
}

func BenchmarkSortSort(b *testing.B) {
    for i := 0; i < b.N; i++ {
        b.StopTimer()
        slice := generateRandomSlice(1000)
        b.StartTimer()
        sort.Sort(IntSlice(slice))
    }
}

结果

E:\Temp\gotmp
go test -bench . -benchmem -count 10
goos: windows
goarch: amd64
pkg: gotmp
cpu: AMD Ryzen 5 3600 6-Core Processor              
BenchmarkSlicesSort-12             29439             38896 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30897             39182 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30460             39426 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30721             39071 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30404             39610 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30872             39212 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30682             39138 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30840             39359 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             29970             39180 ns/op               0 B/op          0 allocs/op
BenchmarkSlicesSort-12             30578             39407 ns/op               0 B/op          0 allocs/op
BenchmarkSortSort-12               17241             68391 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17275             68589 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17451             68513 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17326             69480 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17428             68666 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17457             69060 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17215             69088 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17142             70907 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               16884             69571 ns/op              24 B/op          1 allocs/op
BenchmarkSortSort-12               17376             69809 ns/op              24 B/op          1 allocs/op
PASS
ok      gotmp   50.845s

slices.Sort 在该条件下约快一倍

#
首页      代码      slices.Sort 和 sort.Sort 的简单 benchmark

Zxilly

文章作者

发表回复

textsms
account_circle
email

Zxilly's Blog

slices.Sort 和 sort.Sort 的简单 benchmark
测试代码 package gotmp import ( "math/rand" "slices" "sort" "testing" ) type IntSlice []int func (s IntSlice) Len()…
扫描二维码继续阅读
2024-08-06