且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

BSXFUN 关于关系运算的内存效率

更新时间:2022-12-18 16:58:46

Introduction &测试设置

为了进行记忆测试以查询问题中提出的要点,让我们定义输入 AB强>:

A = rand(M,N)
B = rand(1,N)

这里,MN 是大小参数,并保留为非常大的数字.

Here, M and N are the size parameters and are kept as really large numbers.

我将使用 repmat 进行比较,因为这似乎是 bsxfun 的最接近替代方案.所以,这里的想法是运行 bsxfunrepmat 等效代码并注意内存使用的颠簸从任务管理器(在 Windows 上).

I would be using repmat for comparisons as that seems like the closest alternative to bsxfun. So, the idea here to run the bsxfun and repmat equivalent codes and watch out for the bumps in memory usages from the Task Manager (on Windows).

该解决方案比较了bsxfunrepmat 的运行时效率 得出的结论是,将关系运算与 bsxfun 结合使用具有极大的运行时效率,因此扩展 内存效率 的基础会很有趣.代码>进行比较.

This solution that compared bsxfun and repmat for runtime efficiency led to the conclusions that using relational operations with bsxfun is hugely runtime efficient, so it would be interesting to extend the basis of memory efficiency to the comparisons.

因此,bsxfunrepmat 等价物看起来像这样 -

Thus, the bsxfun and repmat equivalents would look something like these -

REPMAT version:  A == repmat(B,size(A,1),1)
BSXFUN version: bsxfun(@eq,A,B))

结果

在运行 repmatbsxfun 代码后,Windows 任务管理器显示类似这样的内容第一个 bump 表示 repmat 的运行,下一个是 bsxfun一 -

Results

On running the repmat and then bsxfun codes, the Windows Task Manager showed something like this with the first bump denoting the run for repmat and the next one is for the bsxfun one -

repmat 凸起的高度与创建 A 的实际副本时的高度相同.这基本上表明 repmat 进行了 B 的实际复制,然后进行了相等性检查.由于 B 将被复制到更大的浮点数组,因此内存需求非常大,如之前的内存图中所示.另一方面,对于 bsxfun,从它的 bump 高度来看,它似乎没有复制实际的浮点值,这导致了有效的内存使用.

The repmat bump has the same height as that when an actual copy of A is created. This basically shows that repmat makes an actual replication of B and then does the equality check. Since, B is to be replicated to a bigger floating point array, the memory requirements are huge as again shown in the memory graph earlier. On the other hand with bsxfun, from its bump height it seems is not replicating the actual floating point values and that leads to an efficient memory usage.

现在,在将 AB 转换为逻辑数组后,内存使用量激增改成这样-

Now, after converting both A and B to logical arrays, the memory usage bumps changed to this -

因此,这表明 repmat 然后能够优化内存,因为这次复制是逻辑数据类型.

Thus, it suggests that repmat was then able to optimize memory, as this time the replication was of logical datatype.

bsxfun 中使用匿名函数: 可以用 bsxfun 和匿名函数的用法做一些实验看看 MATLAB 在优化内存需求方面是否表现出与内置程序相同的智能.

Using anonymous functions with bsxfun: One can experiment a bit with the anonymous functions usage with bsxfun and see if MATLAB shows the same smartness with it in optimizing memory requirements as with the built-in.

所以,bsxfun(@eq,A,B) 可以替换为 bsxfun(@(k1,k2) k1==k2,A,B).在对浮点输入数组进行操作时,使用此内置和匿名函数实现的结果内存使用情况,导致内存图如下所示 -

So, bsxfun(@eq,A,B) could be replaced by bsxfun(@(k1,k2) k1==k2,A,B). The resultant memory usage with this built-in and anonymous function implementation when operated on floating point input arrays, resulted in a memory graph as shown below -

该图表明匿名函数的使用保持了内置函数的内存效率,即使运行时受到了很大的阻碍.使用其他关系运算代替时,测试结果相似.

The plot indicates that the use of anonymous function keeps the memory efficiency as with the built-in, even though the runtime is hampered quite a bit. The test results were similar when other relational operations were used instead.

在浮点数组上进行关系运算时,使用 bsxfun 绝对优于 repmat为运行时和内存效率.所以,这只是证明还有更多理由选择 bsxfun

When working with relational operations on floating-point arrays, it's definitely preferable to use bsxfun over repmat for both runtime and memory efficiency. So, this just proves that there are more reasons to go with bsxfun!