Bad performance when setting first column element of a matrix
Show older comments
I have a loop in which I read a lot of data and store it in a matrix, like this:
l = numel(c_d); % c_d is data to be stored
typeData.data{j}(row, 1:l) = c_d; % row and j have been determined earlier
The performance of this seemed bad, so I tried pre-allocating the matrix stored in typeData.data{j}. This did not seem to matter so I tried the following: (results of profiling in comments)
l = numel(c_d); % c_d is data to be stored
c_d = double(c_d); % c_d might be a uint8 so cast to double to make sure that doesn't matter.
temp = typeData.data{j};
[r, c] = size(temp);
if cnt > r || l > c
disp('Not allocated'); % Check on allocation. Never hit during profiling!
end
% c_d is a 4 element vector
if l == 4
temp(row, 1) = c_d(1); % Takes 1 to 1.5 s in profiler
temp(row, 2) = c_d(2); % Takes < 0.01 s
temp(row, 3) = c_d(3); % Takes < 0.01 s
temp(row, 4) = c_d(4); % Takes < 0.01 s
local(r, c) = NaN(r, c); % Pre allocate a local matrix
local(row, 1) = c_d(1); % Takes < 0.01 s
local(row, 2) = c_d(2); % Takes < 0.01 s
local(row, 3) = c_d(3); % Takes < 0.01 s
local(row, 4) = c_d(4); % Takes < 0.01 s
else
typeData.data{j}(row, 1:l) = c_d;
end
typeData.data{j} = temp;
This loop is run about 13000 times in the above example. Setting the first column element takes the most time.
I'm wondering why setting the first column element takes so much time. It seems something in the structure of the temp matrix is different than the local matrix, but I have no idea what it can be. According to the debugger, both local and xoop are equal sized matrices using doubles. Can someone shed some light on this?
Remarks:
- The matrix stored in typeData.data{j} is pre-allocated using NaNs, but using something else, like zeros, does not matter.
- Not Pre-allocating the matrix stored in typeData.data{j} does not give a (significant) difference for performance. Pre-allocating a much larger matrix degrades the performance.
- Obviously there is more code around this, but I tried to keep this post small by not pasting all of it ;) Please ask about it!
Accepted Answer
More Answers (0)
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!