MATLAB Tips for Simulation Parinya Sanguansat
Version Selection X86 Faster Smaller X64 Slower Bigger
Section Syntax: % section Run Section Run without save
Save the results Long time processing Power failure Computer freeze and crash TIP Save and continue next time Don't save all variables Too many saves, slow down your work Display progress (Text / GUI) Redundant save file
Example work for i=1:100 result(i) = i end
Save and continue next time if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end for i=startindex:100 result(i) = i; startindex = i; save savedata end
Don't save all variables if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end for i=startindex:100 result(i) = i; startindex = i; save('savedata','startindex','result'); end
Too many saves, slow down your work if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end for i=startindex:100 result(i) = i; if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); end
Display progress if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end endindex = 100; fprintf('\n\n%6.2f%', 0); for i=startindex:endindex result(i) = i; fprintf('\b\b\b\b\b\b\b%6.2f%', i/endindex * 100); pause(1) if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); end
Display progress with GUI if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end endindex = 100; waitbar(startindex/endindex,'Processing...'); fprintf('\n\n%6.2f%', 0); for i=startindex:endindex result(i) = i; waitbar(i/endindex); fprintf('\b\b\b\b\b\b\b%6.2f%', i/endindex * 100); pause(1) if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); end
Redundant save file if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end endindex = 100; waitbar(startindex/endindex,'Processing...'); fprintf('\n\n%6.2f%', 0); for i=startindex:endindex result(i) = i; waitbar(i/endindex); fprintf('\b\b\b\b\b\b\b%6.2f%', i/endindex * 100); pause(1) if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); save('savedata2','startindex','result'); % redundant save data end
Speed up your work Long time processing Using huge memory TIP Display too much information will slow down your work Allocate memory Use small data type first Access row (the first dimension) first Eliminate nested loop Array processing with GPU
Display % display too much information will slow down your work for i=1:100 result(i) = i; if mod(i,10) == 0 result(i) end
Allocation memory memsize = 100; result = zeros(1,memsize); for i=1:memsize result(i) = i; end
Use small data type first memsize = 100; result = zeros(1,memsize,'uint8'); for i=1:memsize result(i) = i; end
Access row (the first index) first A = rand(1000); for j=1:size(A,1) for i=1:size(A,2) A(i,j) = 1; end
Eliminate nested loop A = rand(1000); for i=1:numel(A) A(i) = 1; % row index = mod(i-1,size(A,1))+1 % column index = ceil(i/size(A,1)); end Vectorization Linear indexing
Array processing with GPU X = rand(1000); G = gpuArray(X); % Send data to GPU G2 = G.* G; % Performed on GPU W = gather(G); % Return data to client