Optimizing matlab code by removing nested for loop? -
this code given, takes long run. how make faster removing nested loop?
igroup = 1:length(groupindices) curgroupindex = groupindices(igroup); curchanindices = chanindices{igroup}; curchannames = channames{igroup}; grouppropstruct = propstostruct(propnames{curgroupindex},propvalues{curgroupindex},replace_str,prepend_str,always_prepend); groupstruct = struct('name',groupnames(igroup),'props',grouppropstruct); ichan = 1:length(curchanindices) curchanindex = curchanindices(ichan); chanpropstruct = propstostruct(propnames{curchanindex},propvalues{curchanindex},replace_str,prepend_str,always_prepend); chanstruct = struct('name',curchannames{ichan},'props',chanpropstruct,... 'data',[]); chanstruct.data = data{curchanindex}; groupstruct.(tdms_genvarname2(chanstruct.name,... replace_str,prepend_str,always_prepend)) = chanstruct; end output.(tdms_genvarname2(groupstruct.name,... replace_str,prepend_str,always_prepend)) = groupstruct; end
as 1 said, it's hard without knowing, code actual doing. maybe give toy-example regarding code? help.
anyway, here 4 major points consider, when writing matlab for-loops:
1: of course, use build-in matlab functionality instead of for-loops. written in c/fortran , faster regarding simd, multythreading, etc.
2: for-loops consecutiv , sliceable. consider use parfor loops, use multi-processor functionality on loop.
3: for-loop capsuled in matlab-function? if not, it!, yit-compiler matlab can compile loop byte-code faster!
4: if familiar c++, write mex-function. here can use full potential of machine.
Comments
Post a Comment