optimization - MATLAB Speed Difference in Identical Code -
i have matlab code i've been working with. there 3 sections, a, b, , c. , c can change, b stays same regardless.
what did separate a, b, , c separate .m files (not functions, scripts). creates set of variables, b contains logic, , c contains plotting results. i'll call d file i've done linearly copy/paste entire contents of a, b, , c right after each other.
if run a, b, c, iteration within b goes slowly, 15 seconds per iteration. if run d (just b c pasted in) goes fast, 2 seconds per iteration.
given matlab running exact same code in exact same order, why 2 have such drastically different execution times?
i believe have memory issues. matlab functions input variables pointers, if change data, becomes copy. if a,b,c have large input , output, , each 1 of them modifies small part of data, lot of memory allocations.
for example:
function main() x = imread('peppers.png'); i=1:size(x,1) j=1:size(x,2) x = changepixel(x,i,j); end end imshow(x); end function = changepixel(a,i,j) a(i,j,:) = a(i,j,[3 2 1]); end
this code slow, since changepixel allocates each time new matrix.
sometimes, matlab can detect these kind of operations , use inner optimization. however, 1 cannot know whether take place or not, best avoid kind of operations.
Comments
Post a Comment