-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix64_test.go
132 lines (123 loc) · 3.16 KB
/
matrix64_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package goumem
import (
"math/rand"
"testing"
)
// Global variable to prevent compiler optimizations
var result float64
// In total 100000 matrices of 100x100 elements
// Each element is a float64
// Which means that each matrix is 80000 bytes
// In total 8000000000 bytes
// Which means 7.450580596923828 GB
const (
numMatrices = 100000
rows = 100
cols = 100
)
func BenchmarkCustomMemory(b *testing.B) {
pool, err := NewPoolMatrix64(numMatrices, rows, cols)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var r float64
for pb.Next() {
randVal := rand.Float64()
for i := 0; i < numMatrices; i++ {
matrix := pool.Get()
r += simulateReadWrite(matrix, randVal)
pool.Free(matrix)
}
}
result = r
})
b.StopTimer()
//poolMatrix64, err := NewPoolMatrix64(numMatrices, rows, cols)
//if err != nil {
// b.Fatal(err)
//}
//
//var r float64
//for i := 0; i < b.N; i++ {
// randVal := rand.Float64()
// for j := 0; j < numMatrices; j++ {
// matrix := poolMatrix64.Get()
// r += simulateReadWrite(matrix, randVal)
// poolMatrix64.Free(matrix)
// }
// runtime.GC()
//}
//b.StopTimer() // Stop the timer before doing operations not related to the actual benchmark
//result = r // Assign the final result to the global variable
//_ = fmt.Sprint(result) // Print the result or use it in a way that ensures it's not optimized out
}
func BenchmarkGCMemory(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var r float64
for pb.Next() {
matrix := makeSampleMatrix(rows, cols)
randVal := rand.Float64()
for i := 0; i < numMatrices; i++ {
r += simulateReadWriteGC(matrix, randVal)
}
}
result = r
})
b.StopTimer()
//var r float64
//b.ResetTimer()
//matrices := make([][]float64, numMatrices)
//for i := 0; i < b.N; i++ {
// randVal := rand.Float64()
// for j := 0; j < numMatrices; j++ {
// matrix := makeSampleMatrix(rows, cols)
// r += simulateReadWriteGC(matrix, randVal)
// matrices = matrix
// }
// runtime.GC()
//}
//runtime.KeepAlive(matrices)
//b.StopTimer() // Stop the timer before doing operations not related to the actual benchmark
//result = r // Assign the final result to the global variable
//_ = fmt.Sprint(result) // Print the result or use it in a way that ensures it's not optimized out
}
func simulateReadWrite(matrix *PointerMatrixFloat64, randVal float64) float64 {
var sum float64
var val float64
rows := matrix.Rows()
cols := matrix.Cols()
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
val = matrix.GetRowColValue(i, j)
val *= randVal
matrix.SetRowColValue(i, j, val)
sum += val
}
}
return sum
}
func simulateReadWriteGC(matrix [][]float64, randVal float64) float64 {
var sum float64
for r := range matrix {
for c := range matrix[r] {
val := matrix[r][c]
val *= randVal
matrix[r][c] = val
sum += val
}
}
return sum
}
func makeSampleMatrix(rows, cols int) [][]float64 {
matrix := make([][]float64, rows)
for i := range matrix {
matrix[i] = make([]float64, cols)
for j := range matrix[i] {
matrix[i][j] = rand.Float64()
}
}
return matrix
}