加速重塑/不使用重塑Matlab [英] Speed up reshape/ not use reshape Matlab

查看:72
本文介绍了加速重塑/不使用重塑Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多次这样的操作:

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.

推荐答案

没有简单的方法可以加快速度.如果n超过某个数字(由您相关的缓存大小定义),则将在转置运算符期间对内存访问的方式进行排序.成本实际上是在转置操作中产生的.在下面,我绘制了不同矩阵尺寸的成本.大约有360的跳跃,这与我的处理器上的缓存大小一致.

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.

如果要避免这种情况发生,则需要创建优化缓存"的重新排序策略,即在m * m个图块中执行重新排序,这两个向量都将适合缓存.

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.

这篇关于加速重塑/不使用重塑Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆