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

     1  package lcs
     2  
     3  import (
     4  	"math/rand"
     5  	"strconv"
     6  	"testing"
     7  )
     8  
     9  const (
    10  	N = 160000
    11  	A = 16
    12  )
    13  
    14  var (
    15  	dataA = make([]int64, N)
    16  	dataB = make([]int64, N)
    17  	dataC = make([]int64, N)
    18  )
    19  
    20  func init() {
    21  	r := rand.New(rand.NewSource(0))
    22  	for i := range dataA {
    23  		dataA[i] = r.Int63n(A)
    24  		dataB[i] = r.Int63n(A)
    25  		dataC[i] = dataA[i]
    26  	}
    27  }
    28  func benchmark(b *testing.B, lcs func(a, b []int64) int) {
    29  	for _, size := range []int{10, 100, 1000, 10000} {
    30  		b.Run("Random"+strconv.Itoa(size), func(b *testing.B) {
    31  			for i := 0; i < b.N; i++ {
    32  				_ = lcs(dataA[:size], dataB[:size])
    33  			}
    34  		})
    35  
    36  		b.Run("Full"+strconv.Itoa(size), func(b *testing.B) {
    37  			for i := 0; i < b.N; i++ {
    38  				_ = lcs(dataA[:size], dataC[:size])
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func BenchmarkBasic(b *testing.B) { benchmark(b, Basic) }
    45  func BenchmarkLift(b *testing.B)  { benchmark(b, Lift) }
    46  func BenchmarkWave(b *testing.B)  { benchmark(b, Wave) }