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

9 次查看(过去 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 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by