mat: add benchmarks for trimul
MODIFY
mat/diagonal_test.go
mat/diagonal_test.go
@@ -9,6 +9,8 @@ import (
"reflect"
"testing"
+ "golang.org/x/exp/rand"
+
"gonum.org/v1/gonum/blas/blas64"
)
@@ -458,3 +460,11 @@ func TestDiagonalAtSet(t *testing.T) {
}
}
}
+
+func randDiagDense(size int, rnd *rand.Rand) *DiagDense {
+ t := NewDiagDense(size, nil)
+ for i := 0; i < size; i++ {
+ t.SetDiag(i, rnd.Float64())
+ }
+ return t
+}

MODIFY
mat/triangular_test.go
mat/triangular_test.go
package mat
import (
+ "fmt"
"math"
"reflect"
"testing"
@@ -513,23 +514,69 @@ func TestCopySymIntoTriangle(t *testing.T) {
}
}
-func BenchmarkTriSum1000(b *testing.B) { triSumBench(b, 1000) }
-
var triSumForBench float64
-func triSumBench(b *testing.B, size int) {
- a := randTriDense(size)
- b.ResetTimer()
+func BenchmarkTriSum(b *testing.B) {
+ rnd := rand.New(rand.NewSource(1))
+ for n := 100; n <= 1600; n *= 2 {
+ a := randTriDense(n, rnd)
+ b.Run(fmt.Sprintf("BenchmarkTriSum%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
triSumForBench = Sum(a)
}
+ })
+ }
+}
+
+var triProductForBench *TriDense
+
+func BenchmarkTriMul(b *testing.B) {
+ rnd := rand.New(rand.NewSource(1))
+ for n := 100; n <= 1600; n *= 2 {
+ triProductForBench = NewTriDense(n, Upper, nil)
+ a := randTriDense(n, rnd)
+ c := randTriDense(n, rnd)
+ b.Run(fmt.Sprintf("BenchmarkTriMul%d", n), func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ triProductForBench.MulTri(a, c)
+ }
+ })
+ }
+}
+
+func BenchmarkTriMulDiag(b *testing.B) {
+ rnd := rand.New(rand.NewSource(1))
+ for n := 100; n <= 1600; n *= 2 {
+ triProductForBench = NewTriDense(n, Upper, nil)
+ a := randTriDense(n, rnd)
+ c := randDiagDense(n, rnd)
+ b.Run(fmt.Sprintf("BenchmarkTriMulDiag%d", n), func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ triProductForBench.MulTri(a, c)
+ }
+ })
+ }
+}
+
+func BenchmarkTriMul2Diag(b *testing.B) {
+ rnd := rand.New(rand.NewSource(1))
+ for n := 100; n <= 1600; n *= 2 {
+ triProductForBench = NewTriDense(n, Upper, nil)
+ a := randDiagDense(n, rnd)
+ c := randDiagDense(n, rnd)
+ b.Run(fmt.Sprintf("BenchmarkTriMul2Diag%d", n), func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ triProductForBench.MulTri(a, c)
+ }
+ })
+ }
}
-func randTriDense(size int) *TriDense {
+func randTriDense(size int, rnd *rand.Rand) *TriDense {
t := NewTriDense(size, Upper, nil)
for i := 0; i < size; i++ {
for j := i; j < size; j++ {
- t.SetTri(i, j, rand.Float64())
+ t.SetTri(i, j, rnd.Float64())
}
}
return t

mat: add benchmarks for trimul