矩阵重新分配
我试图压缩脚本的执行时间,避免无用的大矩阵重新分配。 像这样的操作
B = A;
几乎不会产生开销,因为 B
将指向 A
的相同结构,并且 Matlab 在发生更新之前不会分配新的结构。
但是像这样的操作呢?
longVector = longVector(1:n);
它会简单地更新longVector
结构以指向已经存在的数据子集,还是会导致分配一个新向量并丢弃旧向量(更耗时)?
I'm trying to squeeze execution time on a script avoiding useless big-matrix reallocation.
An operation like
B = A;
causes little overhead since B
will point at the same structure of A
, and Matlab won't allocate a new one until an update occurs.
But what about an operation like this?
longVector = longVector(1:n);
Will it simply update longVector
structure to point to the already existing subset of datas or (more time expensive) will it cause to allocate a new vector and trash the old one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定这是否一定更好(我还没有测试差异),但您可以尝试
longVector(n+1:end)=[]
。我很确定这不会分配新变量。I'm not sure if this would be better necessarily (I haven't tested the difference), but you could instead try
longVector(n+1:end)=[]
. I'm pretty sure that won't allocate a new variable.我相信 MATLAB,
除非后面的
占用至少两倍的内存。
至少在 Mac 上以及几个版本之前(2009 年左右)都是如此。
顺便说一句,目前还不清楚你想在这里实现什么目标?
为什么这会提高你的表现?
I believe in MATLAB
unless followed by
uses up at least twice the memory.
At least on mac and up to a few versions back (2009 or so) it was so.
By the way it is not clear what you are trying to achieve here?
Why this would improve your performance?
是的,它会缩小分配的块,但随着时间的推移,它会导致碎片(在 Windows 系统上)。
Yes it will shrink the allocated block, but it will lead to fragmentation (on a Windows system) over time.