From 3cd177b99e6505bcf1127adfafec7fa0adf714af Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 25 Jan 2025 17:17:11 +0700 Subject: 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. --- lib/slices/benchmark_test.go | 20 ++++++++------------ 1 file 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) } -- cgit v1.3