Improving*MATLAB!processing*efficiency! Tic$Toc$Function$

advertisement
Improving*MATLAB!processing*efficiency!
Tic$Toc$Function$
These%operations%run%together%to%provide%timings%of%functions%or%parts%of%functions.%Timings%
are%provided%in%‘real7time’%or%‘wall7time’.%%
%
Example(1(
tic%%%%%%%%%%%%%%%%%% %
%
A%=%rand(1200,%440);%%
B%=%rand(1200,%440);%%
toc%%%%%%%%%%%%%%%%%% %
%
C%=%A'.*B';%%
toc%%%%%%%%%%%%%%%%% %
%
%
Output:%%
%
%
%start%timer%
%
%
%save%time%interval%
%
%
%%save%time%interval%
Elapsed%time%is%0.032489%seconds.%
Elapsed%time%is%0.051778%seconds.%
%
In%the%above%function,%tic%denotes%the%start%of%the%timer.%The%rand%function%provides%a%matrix%of%
the%given%size%of%randomly%generated%numbers.%Hence,%C%is%the%multiplication%of%these%randomly%
generated%matrices%‘A’%and%‘B’.%The%first%toc%will%record%how%long%it%took%from%‘tic’%till%that%‘toc’%
i.e.%how%long%it%took%to%create%the%two%matrices.%The%second%toc%records%how%long%it%took%from%
‘tic’%till%that%‘toc’,%and%has%no%connection%with%the%previous%‘toc’.%Hence,%to%calculate%how%long%it%
took%for%C%to%be%calculated,%we%could%subtract%the%two%outputs%or%we%could%simply%add%another%
line%with%‘tic’%after%the%first%‘toc’.%
MATLAB$Profiler$
The%MATLAB%Profiler%is%an%interface%that%gives%information%on%the%execution%time%of%different%
functions%within%a%MATLAB%code%file.%This%allows%you%to%view%which%subtask%is%taking%longest%
within%function%and%hence%allows%you%to%improve%your%code.%It%also%outputs%the%number%of%
times%a%function%was%called%and%information%about%parent%and%child%functions.%It%is%a%little%more%
complex%than%the%tic7toc%function%as%by%default%it%records%how%long%computations%took%in%‘CPU%
time’.%Unlike%‘wall%time’,%CPU%time%records%precisely%how%long%your%computer’s%processor%took%
to%compute%the%function%excluding%any%other%processes%that%it%was%running%simultaneously.%
This%provides%more%accurate%recordings%of%the%function.%However,%the%profiler%can%be%modified%
to%record%in%real%time.%
%
Example(2(
Consider%the%very%simple%function%on%the%left.%‘Profile%on’%simply%turns%
on%the%profiler%and%profile%viewer%provides%us%with%the%profile%
summary%below.%(The%magic%function%in%this%case%produces%a%4x4%
matrix%in%which%all%the%columns,%rows%and%diagonals%all%add%up%to%the%same%number.%This%
matrix%was%plotted%on%a%graph.)%
%
We%can%see%that%
plotting%the%graph%
took%up%almost%all%of%
the%time%in%computing%
this%function.%Other%
modifications%can%be%
made%to%the%function%
for%example,%pausing%
or%restarting%the%timer%
etc:%
%
• %‘profile%–
history’%means%the%exact%history%of%function%calls%is%also%recorded.%Specify%size%with%‘7
historysize’%(default%1%million)%%
• ‘profile7nohistory’%allows%one%to%stop%further%recording%of%any%history%
• ‘profile%–timer’%allows%you%to%change%between%‘CPU’%and%‘real’%time.%E.g.%‘profile%–timer%
real’%or%‘profile%–timer%CPU’.%
• %‘profile(‘status’)’%returns%the%current%status%of%each%of%the%fields%of%the%profile%function.%
• ‘profile%–resume’%allows%you%to%restart%the%profile%timer%without%losing%any%history%
• ‘profile%–clear’%clears%the%history%
%
Another%way%to%run%the%profiler%is%by%clicking%the%
button%‘run%and%time’.%This%would%simply%time%the%
whole%function%in%CPU%time.%
For3loops$vs$Vector$Computations$
Simply%put,%if%processing%time%of%a%function%is%of%concern%then%replacing%for7loops%with%a%simple%
‘vector’%computation%would%be%a%beneficial.%Consider%the%below%long7winded%but%simple%
function%on%the%left.%It%can%be%replaced%with%the%function%on%the%right%using%vector%computation%
(computing%vectors)%which%speeds%up%computations%considerably%(c=e,%d=f):%
Elapsed%time%is%0.242919%seconds%% %
%%%%%%%%%
%
%%%%%%%%Elapsed%time%is%0.000067%seconds%
Column$vs$row$vectors$
MATLAB%processes%column%vectors%faster%than%row%vectors%because%it%considers%arrays%in%a%
system%known%as%‘column7major%order’.%This%means%an%array%is%read%column%by%column,%rather%
than%row%by%row.%So%for%example,%for%MATLAB%to%compute%one%row%within%a%matrix,%it%would%go%
through%every%column%and%go%down%each%column%till%it%gets%to%the%specific%nth%value%where%n%is%
the%row%number.%E.g.%
is%read%as%
%rather%than%123456.%
Hence,%column%vectors%are%processed%faster%as%all%of%the%numbers%is%in%the%same%column%as%
opposed%to%a%row%vector%which%takes%extra%computational%power%to%transpose.%
Download