performance - Why in Matlab .* operator is faster than * for a scalar in some case? -
consider following code :
a=rand(10000); b=rand(10000); tic; 2*(a<b); toc; tic; 2.*(a<b); toc; the result :
elapsed time 0.938957 seconds. elapsed time 0.426517 seconds. why second case twice faster first case ?
edit : obtain same result size of matrix, whatever order test it, with
(a<b).*3.56 vs (a<b)*3.56 for example, not with
(a.*b)*2 vs (a.*b).*2 or
(a*b)*2 vs (a*b).*2 it seems there link logical array, because have same result with
(a&b)*2 vs (a&b).*2 computer : r2015b, windows 10 x64
i suggest performing more strict check of performance. put test in named function let matlab optimize both pieces of code, , run both codes several times, choosing quickest runtime. hunch should take same amount of time, although can't check right reasonable matrix sizes. here's i'd do:
function product_timing(n) a=rand(n); b=rand(n); tmin=inf; k=1:10 tic; res1=2*(a<b); t=toc; if t<tmin tmin=t; end end disp(tmin); tmin=inf; k=1:10 tic; res2=2.*(a<b); t=toc; if t<tmin tmin=t; end end update
on r2012b there doesn't seem marked difference between 2 methods. however, others have indicated, r2015b new execution engine makes difference.
while i'm still unsure answer, let me collect feedback @x1hgg1x (comments on both answer , question) , @luismendo (in chat), elaborate on ignorance:
c*3.56integer factor (number of threads?) of times slowerc.*3.56(with scalar) ifclogical, not ifcuint8ordouble- the same holds true vectors, not square matrices
as it's stated on matlab product page:
run programs faster redesigned matlab® execution engine.
the improved architecture uses just-in-time (jit) compilation of matlab code single execution pathway. engine offers improved language quality , provides platform future enhancements.
specific performance improvements include made to:
...
element-wise math operations
the execution of many element-wise math operations optimized. these operations element-by-element arithmetic operations on arrays such following:
>> b = ((a+1).*a)./(5-a);
however, looking @ docs of .* , *, can't see information relating problem. note array vs matrix operations concerning array operations .*:
if 1 operand scalar , other not, matlab applies scalar every element of other operand. property known scalar expansion because scalar expands array of same size other input, operation executes 2 arrays.
and doc of matrix product * says
if @ least 1 input scalar, a*b equivalent a.*b , commutative.
as see, equivalence of a*b , a.*b arguable. well, equivalent mathematically, strange going on.
due above notes, , fact performance difference arises logical arrays, consider undocumented feature. would've thought it's related logicals occupying 1 byte each, speed-up doesn't manifest uint8 arrays. suggest since logicals contain information in single bit, internal optimization possible. still doesn't explain why mtimes doesn't this, , it's surely related internal workings of times vs mtimes.
one thing sure: times doesn't fall on mtimes scalar operands (maybe should?). since in r2012b whole effect missing, believe optimized array operations of new execution engine mentioned above treat logical arrays separately, allowing special case of scalar.*logical_array sped up, same optimization missing mtimes.
Comments
Post a Comment