aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-01-25 17:17:11 +0700
committerShulhan <ms@kilabit.info>2025-01-25 17:17:11 +0700
commit3cd177b99e6505bcf1127adfafec7fa0adf714af (patch)
tree55d7e5a40421cbeec946b6c57dc72e6308688e49
parent2a0694d2fa577574b505c4635eb8a824eaf88ddc (diff)
downloadpakakeh.go-3cd177b99e6505bcf1127adfafec7fa0adf714af.tar.xz
lib/slices: copy the random test data on each run
Since the "sorts.Ints", "IndirectSort", and "InplaceMergesort" store the sorted value in-place, it is incorrect to run the sort again using the same slice, the second run will use sorted input. While at it, call ResetTime in testing InplaceMergesort for float64.
-rw-r--r--lib/slices/benchmark_test.go20
1 files changed, 8 insertions, 12 deletions
diff --git a/lib/slices/benchmark_test.go b/lib/slices/benchmark_test.go
index 5bbde4e4..2b7f4576 100644
--- a/lib/slices/benchmark_test.go
+++ b/lib/slices/benchmark_test.go
@@ -17,36 +17,32 @@ func BenchmarkSort_int(b *testing.B) {
var data = make([]int, n)
generateRandomInts(data, n)
- var dataIndirect = make([]int, n)
- copy(dataIndirect, data)
-
- var dataInplaceMergesort = make([]int, n)
+ var dataUnsorted = make([]int, n)
var inplaceIdx = make([]int, n)
- copy(dataInplaceMergesort, data)
- var dataSortInts = make([]int, n)
- copy(dataSortInts, data)
b.ResetTimer()
b.Run(`sort.Ints`, func(b *testing.B) {
for x := 0; x < b.N; x++ {
- sort.Ints(dataSortInts)
+ copy(dataUnsorted, data)
+ sort.Ints(dataUnsorted)
}
})
b.Run(`IndirectSort`, func(b *testing.B) {
for x := 0; x < b.N; x++ {
- slices.IndirectSort(dataIndirect, true)
+ copy(dataUnsorted, data)
+ slices.IndirectSort(dataUnsorted, true)
}
})
b.Run(`InplaceMergesort`, func(b *testing.B) {
for x := 0; x < b.N; x++ {
- slices.InplaceMergesort(dataInplaceMergesort,
+ copy(dataUnsorted, data)
+ slices.InplaceMergesort(dataUnsorted,
inplaceIdx, 0, n, true)
}
})
-
}
func BenchmarkInplaceMergesort_float64(b *testing.B) {
@@ -65,7 +61,7 @@ func BenchmarkInplaceMergesort_float64(b *testing.B) {
}
var size = len(slice)
var ids = make([]int, size)
-
+ b.ResetTimer()
for i := 0; i < b.N; i++ {
slices.InplaceMergesort(slice, ids, 0, size, true)
}