I have been benchmarking some of my code, and to my surprise it looks like two simple functions are taking quite some time to compute. Ok, they are called many many times inside a while loop, but I was not expecting this.
They are quite simple functions

given a 2-d slice (matrix NxN, where N is in the range of 2-50), and a matrix column index, return the value in that column as a slice

func view(mm [][]int, columnIndex int) []int {
column := make([]int, len(mm))
for i, row := range mm {
column[i] = row[columnIndex]
}
return column
}

given a 2-d slice (matrix NxN, where N is in the range of 2-30), return the matrix column sums as a slice

func colSums(mm [][]int) []int {
sum := make([]int, len(mm[0]))
for i := range mm[0] {
psum := 0
for j := range mm {
psum = psum + mm[j][i]
}
sum[i] = psum
}
return sum
}

If you transpose your matrices, ie. make the outer slices columns instead of rows, then youâ€™ll have better locality of reference and hit caches more consistently.

Another option is to allocate the entire matrix in one contiguous slice, and then construct sub-slices for the rows or columns.