What is faster, numpy transpose or invert indexes?

I have a dynamic programming algorithm (modified by Needleman-Wunsch) that requires the same basic calculation twice, but the calculation is performed in the orthogonal direction a second time. For example, from a given cell (i, j) in the scoreMatrix matrix, I want to calculate both the value from the β€œup” values ​​from (i, j) and the value from the values ​​in β€œleft” of (i, k). To reuse the code, I used a function in which in the first case I send the parameters i, j, scoreMatrix, and in the next case I send j, i, scoreMatrix.transpose (). Here is a very simplified version of this code:

def calculateGapCost(i,j,scoreMatrix,gapcost): return scoreMatrix[i-1,j] - gapcost ... gapLeft = calculateGapCost(i,j,scoreMatrix,gapcost) gapUp = calculateGapCost(j,i,scoreMatrix.transpose(),gapcost) ... 

I realized that I could alternatively send a function that in one case would go through the arguments (i, j) when retrieving the value from scoreMatrix, and in the other case would cancel them with (j, i), and not rearrange the matrix every time .

 def passThrough(i,j,matrix): return matrix[i,j] def flipIndices(i,j,matrix): return matrix[j,i] def calculateGapCost(i,j,scoreMatrix,gapcost,retrieveValue): return retrieveValue(i-1,j,scoreMatrix) - gapcost ... gapLeft = calculateGapCost(i,j,scoreMatrix,gapcost,passThrough) gapUp = calculateGapCost(j,i,scoreMatrix,gapcost,flipIndices) ... 

However, if numpy transpose uses some functions that I don’t know about to transpose in just a few operations, it is possible that the transpose is actually faster than my idea of ​​a pass-through function. Can someone tell me what will be faster (or if there is a better method that I did not think about)?

The actual method will call retrieveValue 3 times and includes 2 matrices that will be referenced (and thus transposed using this approach).

+6
source share
1 answer

In NumPy, transposition returns a view with a different shape and strides . It does not concern data.

Therefore, you will most likely find that both approaches have the same performance, since in essence they are exactly the same.

However, the only way to make sure that you need to compare both.

+9
source

All Articles