BSXFUN 关于关系运算的内存效率 [英] BSXFUN on memory efficiency with relational operations

查看:22
本文介绍了BSXFUN 关于关系运算的内存效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里研究的主要有两件事-

There are mainly two things I would like to research on about here -

  • 有六种内置关系运算可用于 bsxfun :@eq (equal)@ne(不等于)@lt(小于)@le(小于或等于)@gt(大于)@ge(大于或等于).很多时候我们将它们用于浮点数和关系运算,它们输出逻辑数组.所以,这让我很好奇,在对浮点数使用这些关系运算时,bsxfun 的固有扩展是否涉及输入元素的实际复制,这正是我的第一个问题.

  • There are six built-in relational operations for use with bsxfun : @eq (equal), @ne (not-equal), @lt (less-than), @le (less-than or equal), @gt (greater-than) and @ge (greater-than or equal). Lots of times we use them on floating point numbers and being relational operations, they output logical arrays. So, it got me curious, if the inherent expansion with bsxfun when using these relational operations on floating point numbers involve actual replication of input elements and that is precisely my first question.

我还想知道这个内存效率问题如何转化为 匿名函数bsxfun 一起使用时,同样是关系运算的情况.

I would also like to know how this memory efficiency issue translates to the anonymous functions when used with bsxfun, again with the case of relational operations.

这是受到对 执行的runtime/speedup 测试的启发>比较 BSXFUN 和 REPMAT.

推荐答案

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!

这篇关于BSXFUN 关于关系运算的内存效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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