matlab - BSXFUN on memory efficiency with relational operations -
there 2 things research on here -
there 6 built-in relational operations use
bsxfun:@eq (equal),@ne (not-equal),@lt (less-than),@le (less-than or equal),@gt (greater-than),@ge (greater-than or equal). lots of times use them on floating point numbers , being relational operations, output logical arrays. so, got me curious, if inherent expansionbsxfunwhen using these relational operations on floating point numbers involve actual replication of input elements , precisely first question.i know how memory efficiency issue translates
anonymous functionswhen usedbsxfun, again case of relational operations.
this inspired runtime/speedup tests performed comparing bsxfun , repmat.
introduction & test setup
to perform memory tests inquire points raised in question, let's define inputs a , b:
a = rand(m,n) b = rand(1,n) here, m , n size parameters , kept large numbers.
i using repmat comparisons seems closest alternative bsxfun. so, idea here run bsxfun , repmat equivalent codes , watch out bumps in memory usages task manager (on windows).
this solution compared bsxfun , repmat runtime efficiency led conclusions using relational operations bsxfun hugely runtime efficient, interesting extend basis of memory efficiency comparisons.
thus, bsxfun , repmat equivalents these -
repmat version: == repmat(b,size(a,1),1) bsxfun version: bsxfun(@eq,a,b)) results
on running repmat , bsxfun codes, windows task manager showed first bump denoting run repmat , next 1 bsxfun 1 -

the repmat bump has same height when actual copy of a created. shows repmat makes actual replication of b , equality check. since, b replicated bigger floating point array, memory requirements huge again shown in memory graph earlier. on other hand bsxfun, bump height seems not replicating actual floating point values , leads efficient memory usage.
now, after converting both a , b logical arrays, memory usage bumps changed -

thus, suggests repmat able optimize memory, time replication of logical datatype.
using anonymous functions bsxfun: 1 can experiment bit anonymous functions usage bsxfun , see if matlab shows same smartness in optimizing memory requirements built-in.
so, bsxfun(@eq,a,b) replaced bsxfun(@(k1,k2) k1==k2,a,b). resultant memory usage built-in , anonymous function implementation when operated on floating point input arrays, resulted in memory graph shown below -

the plot indicates use of anonymous function keeps memory efficiency built-in, though runtime hampered quite bit. test results similar when other relational operations used instead.
conclusions
when working relational operations on floating-point arrays, it's preferable use bsxfun on repmat both runtime , memory efficiency. so, proves there more reasons go bsxfun!
Comments
Post a Comment