Fastest alternative to |y = ones(size(x))| ?

조회 수: 9 (최근 30일)
Naor Movshovitz
Naor Movshovitz 2016년 7월 29일
답변: Image Analyst 2016년 7월 30일
The profiler red flagged this line, in a small function that is called by an inner-loop computation:
...
if n == 0
y = ones(size(x));
else if n == 1
...
end
I must clarify that this line is not just pre-allocation! I really need the variable y to be the same shape as x and hold 1 in each element. Ideally x can be of any dimensions but if pressed I will admit it is unlikely to be anything but a scalar, vector, or 2D matrix.
I can think of so many possible alternatives, and the practical problem is mostly solved, so this question is mostly for curiosity's sake. I am interested in the fastest method and also in the analysis of why it is fastest. Any ideas?
Thanks, -naor
  댓글 수: 1
James Tursa
James Tursa 2016년 7월 29일
편집: James Tursa 2016년 7월 29일
There really isn't much going on with that line, so I doubt you will be able to improve it much, if at all, with other formulations. The only advantage one formulation might have over another that I can think of is multi-threading in the background for large sizes. Is the profiler flagging this as dominating the runtime? What else is going on around that line?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2016년 7월 29일
My timings cannot distinguish between 1 + zeros(size(x)) vs ones(size(x)) . The hack of using 1 + 0*x is slower.
If you are doing that calculation in a tight loop, then that hints that you might be doing it with x the same size many times in a row. In that case, do the ones() once with the appropriate size, assign to a variable, and then copy from the variable inside the loop. If it is not always the same size within the inner loop but is a small number of sizes, you could keep a cache. You could possibly even pre-process your list of x values to find the distinct sizes, calculate for those sizes before the loop, and then copy out of the appropriate cell array location inside the loop.
  댓글 수: 1
Naor Movshovitz
Naor Movshovitz 2016년 7월 30일
There are so many hacks to try though, and the timing is tricky. You are right the the fastest hack might depend on the size of x. For example in my case the size of x is modest so the difference between
y = ones(size(x));
and
y = ones([12, 48]);
say, is noticeable. Something like
y = x; y(:) = 1;
works but is not noticeably faster. I expect there is not a single hack that is best for every size of x.
And of course you are right that there are workarounds to allocating inside the loop.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 7월 30일
Try using "elseif" instead of "else if"
elseif n == 1

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by