# Code speed up: can any of these 2 simple function be faster?

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

1. 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
}
``````
1. 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))
for i := range mm {
psum := 0
for j := range mm {
psum = psum + mm[j][i]
}
sum[i] = psum
}
return sum
}
``````

Can any be done better/faster?

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.

did you benchmark the code?
Can you compare it to the following code?

1. 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 := range mm {
column[i] = mm[i][columnIndex]
}
return column
}
``````
1. 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))
for i := range mm {
for j := range mm {
sum[i] += mm[j][i]
}
}
return sum
}
``````