BSXFUN关于关系操作的内存效率 [英] BSXFUN on memory efficiency with relational operations
问题描述
我想在这里主要研究两件事-
There are mainly two things I would like to research on about here -
-
与
bsxfun
一起使用的六个内置关系操作:@eq (equal)
,@ne (not-equal)
,@lt (less-than)
,@le (less-than or equal)
,@gt (greater-than)
和@ge (greater-than or equal)
.很多时候,我们将它们用于浮点数并作为关系运算,它们输出逻辑数组.因此,令我感到好奇的是,在对浮点数使用这些关系运算时,使用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 withbsxfun
when using these relational operations on floating point numbers involve actual replication of input elements and that is precisely my first question.
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
测试的启发,该测试针对 Comparing BSXFUN and REPMAT
> .
推荐答案
简介&测试设置
要执行内存测试以查询问题中提出的要点,让我们定义输入 A
和 B
:
Introduction & Test Setup
To perform memory tests to inquire about the points raised in the question, let's define the inputs A
and B
:
A = rand(M,N)
B = rand(1,N)
此处, M
和 N
是大小参数,并保留为非常大的数字.
Here, M
and N
are the size parameters and are kept as really large numbers.
我将使用 repmat
进行比较,因为这似乎是 bsxfun
的最接近的替代方法.因此,这里的想法是运行 bsxfun
和 repmat
等效代码,并从任务管理器(在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).
此解决方案将bsxfun
和repmat
的运行时效率进行了比较,得出的结论是,将关系运算与 bsxfun
具有极大的运行时效率,因此将 memory efficiency
的基础扩展到比较中将很有趣.
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.
因此,等效的 bsxfun
和 repmat
看起来像这样-
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))
结果
在运行 repmat
然后运行 bsxfun
代码时,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
,从其凸起高度来看,似乎并没有复制实际的浮点值,从而导致有效的内存使用.
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.
现在,将 A
和 B
都转换为逻辑数组后,内存使用量颠簸更改为--
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屋!