github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/sorts/sort_test.go (about)

     1  package sort_test
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/shawnsmithdev/zermelo/zuint64"
     8  
     9  	"github.com/egonelbre/exp/sorts/dpsortint"
    10  	"github.com/egonelbre/exp/sorts/rdxsort"
    11  	"github.com/egonelbre/exp/sorts/stdsortint"
    12  	"github.com/zeebo/pcg"
    13  )
    14  
    15  func bench(b *testing.B, size int, algo func([]int), name string) {
    16  	rng := pcg.New(uint64(0))
    17  	data := make([]int, size)
    18  
    19  	b.ResetTimer()
    20  	for i := 0; i < b.N; i++ {
    21  		for i := 0; i < len(data); i++ {
    22  			data[i] = int(rng.Uint64())
    23  		}
    24  		algo(data)
    25  	}
    26  }
    27  
    28  func benchRadix(b *testing.B, size int, algo func(src, dst []uint64), name string) {
    29  	rng := pcg.New(uint64(0))
    30  	data := make([]uint64, size)
    31  	buf := make([]uint64, len(data))
    32  
    33  	b.ResetTimer()
    34  	for i := 0; i < b.N; i++ {
    35  		for i := 0; i < len(data); i++ {
    36  			data[i] = rng.Uint64()
    37  		}
    38  		algo(data, buf)
    39  	}
    40  }
    41  
    42  func BenchmarkRandomOverhead1e4(b *testing.B) { benchRadix(b, 1e4, func(x, y []uint64) {}, "Overhead") }
    43  func BenchmarkRandomOverhead1e6(b *testing.B) { benchRadix(b, 1e6, func(x, y []uint64) {}, "Overhead") }
    44  
    45  func BenchmarkStdSort1e2(b *testing.B) { bench(b, 1e2, sort.Ints, "Std") }
    46  func BenchmarkStdSort1e4(b *testing.B) { bench(b, 1e4, sort.Ints, "Std") }
    47  func BenchmarkStdSort1e6(b *testing.B) { bench(b, 1e6, sort.Ints, "Std") }
    48  
    49  func BenchmarkStdSortInt1e2(b *testing.B) { bench(b, 1e2, stdsortint.Sort, "StdInt") }
    50  func BenchmarkStdSortInt1e4(b *testing.B) { bench(b, 1e4, stdsortint.Sort, "StdInt") }
    51  func BenchmarkStdSortInt1e6(b *testing.B) { bench(b, 1e6, stdsortint.Sort, "StdInt") }
    52  
    53  func BenchmarkDpSort1e2(b *testing.B) { bench(b, 1e2, dpsortint.Sort, "Dp") }
    54  func BenchmarkDpSort1e4(b *testing.B) { bench(b, 1e4, dpsortint.Sort, "Dp") }
    55  func BenchmarkDpSort1e6(b *testing.B) { bench(b, 1e6, dpsortint.Sort, "Dp") }
    56  
    57  func BenchmarkRdxSort1e4(b *testing.B)        { benchRadix(b, 1e4, rdxsort.Uint64, "Rdx") }
    58  func BenchmarkRdxSort1e6(b *testing.B)        { benchRadix(b, 1e6, rdxsort.Uint64, "Rdx") }
    59  func BenchmarkZermeloRdxSort1e4(b *testing.B) { benchRadix(b, 1e4, zuint64.SortBYOB, "Zrdx") }
    60  func BenchmarkZermeloRdxSort1e6(b *testing.B) { benchRadix(b, 1e6, zuint64.SortBYOB, "Zrdx") }