Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster, numpy transpose or flip indices?

I have a dynamic programming algorithm (modified Needleman-Wunsch) which requires the same basic calculation twice, but the calculation is done in the orthogonal direction the second time. For instance, from a given cell (i,j) in matrix scoreMatrix, I want to both calculate a value from values "up" from (i,j), as well as a value from values to the "left" of (i,j). In order to reuse the code I have used a function in which in the first case I send in parameters i,j,scoreMatrix, and in the next case I send in j,i,scoreMatrix.transpose(). Here is a highly simplified version of that 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 in a function that would in the one case pass through arguments (i,j) when retrieving a value from scoreMatrix, and in the other case reverse them to (j,i), rather than transposing the matrix each 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 features I'm unaware of to do the transpose in just a few operations, it may be that transpose is in fact faster than my pass-through function idea. Can anyone tell me which would be faster (or if there is a better method I haven't thought of)?

The actual method would call retrieveValue 3 times, and involves 2 matrices that would be referenced (and thus transposed if using that approach).

like image 635
Todd Gillette Avatar asked Jan 28 '13 20:01

Todd Gillette


1 Answers

In NumPy, transpose returns a view with a different shape and strides. It does not touch the data.

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

However, the only way to be sure is to benchmark both.

like image 174
NPE Avatar answered Nov 18 '22 12:11

NPE