mat: add benchmarks for Sum with Dense, SymDense, TriDense and VecDense
MODIFY
mat/dense_test.go
mat/dense_test.go
@@ -2093,3 +2093,15 @@ func denseMulTransSymBench(b *testing.B, size int, rho float64) {
wd = &n
}
}
+
+func BenchmarkDenseSum1000(b *testing.B) { denseSumBench(b, 1000) }
+
+var denseSumForBench float64
+
+func denseSumBench(b *testing.B, size int) {
+ a, _ := randDense(size, 1.0, rand.NormFloat64)
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ denseSumForBench = Sum(a)
+ }
+}

MODIFY
mat/symmetric_test.go
mat/symmetric_test.go
@@ -735,3 +735,29 @@ func TestPowPSD(t *testing.T) {
}
}
}
+
+func BenchmarkSymSum1000(b *testing.B) { symSumBench(b, 1000) }
+
+var symSumForBench float64
+
+func symSumBench(b *testing.B, size int) {
+ a := randSymDense(size)
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ symSumForBench = Sum(a)
+ }
+}
+
+func randSymDense(size int) *SymDense {
+ backData := make([]float64, size*size)
+ for i := 0; i < size; i++ {
+ backData[i*size+i] = rand.Float64()
+ for j := i + 1; j < size; j++ {
+ v := rand.Float64()
+ backData[i*size+j] = v
+ backData[j*size+i] = v
+ }
+ }
+ s := NewSymDense(size, backData)
+ return s
+}

MODIFY
mat/triangular_test.go
mat/triangular_test.go
@@ -512,3 +512,25 @@ 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()
+ for i := 0; i < b.N; i++ {
+ triSumForBench = Sum(a)
+ }
+}
+
+func randTriDense(size int) *TriDense {
+ t := NewTriDense(size, Upper, nil)
+ for i := 0; i < size; i++ {
+ for j := i; j < size; j++ {
+ t.SetTri(i, j, rand.Float64())
+ }
+ }
+ return t
+}

MODIFY
mat/vector_test.go
mat/vector_test.go
@@ -561,3 +561,15 @@ func randVecDense(size, inc int, rho float64, rnd func() float64) *VecDense {
},
}
}
+
+func BenchmarkVectorSum100000(b *testing.B) { vectorSumBench(b, 100000) }
+
+var vectorSumForBench float64
+
+func vectorSumBench(b *testing.B, size int) {
+ a := randVecDense(size, 1, 1.0, rand.NormFloat64)
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ vectorSumForBench = Sum(a)
+ }
+}

mat: add benchmarks for Sum with Dense, SymDense, TriDense and VecDense