问题
I have this operation which is called multiple times:
longRowVector;
matrix = reshape(longRowVector, n, n)';
answer = matrix(:);
This operation using reshape is slow. Is there a way to get to answer without using reshape.
回答1:
There is no easy way to speed that up. if n exceeds a certain number (defined by your relevant cache size), the way in which the memory accesses will be ordered during the transpose operator. The cost is actually create in the transpose operation. Below i plot this cost for different matrix sizes. There is a jump at around 360, which is consistent with the cache size on my processor.
If you want to avoid this hit, then you need to create your "cache-optimized" reordering strategy, i.e. perform the reordering in m*m tiles where both of the vectors will fit in the cache.
来源:https://stackoverflow.com/questions/34831118/speed-up-reshape-not-use-reshape-matlab