2-1 ????算? JIT ??

b 6.5 eAMATLAB {ҬO@ӶDzΪĶ]Interpreter^Ab MATLAB{XɡA|iUCʧ@G

  1. vC{Xഫ p-codeAoO MATLAB iHֳtŪ榡C
  2. ﲣͪ p-code ivCC

ѩbC@C p-code ɡA٥]t@ǡug`}Pv]Overhead^ApGCݭnjqBAoǸg`}PN|oܷLpӤ|ֵ{tסCϤApGCuO²檺BAg`}PҴN|۹ﴣAVOApGCObj餺A{Xt״N|QoǸg`}PjTCC

]²檺Ab MATLAB 6.5 eAYn[ְtסANnɶqϥΰj]Ҧp for jBwhile j鵥^AӺɶqΦVqƹB]Vectorized operations^AHCg`}PҡAרOnɶqϥ MATLAB تOӧBAɶqקKϥΰjC

b 6.5 AMATLAB ޶iF JIT (Just-In-Time) sĶ޳NA² JIT [t]JIT-Accelerator^AjTaFj骺IJvA]ϱo@{XgAiHϥε{]pv̼x覡AάOϥγ̯Ntk믫覡ӧe{AӤFIJvBjjϥΦVqƹBAy{X@xCboرpUAP_O_ݭnϥΦVqƵ{XANܦOC

JIT [tDn\AiHCXpUG

  1. JIT [tiNC@C MATLAB {XনuOv]Native Machine Instructions^AӤAϥ p-codeA]٥hj p-code |J쪺g`}PC
  2. JIT [t]| Intel X86 D Windows Linux i{X̨ΤơC

ƹWAJIT [tiHⳡG

  1. Just-In-Time Code GenerationGN p-code নOC
  2. Runtime Type AnalysisGƫARAHKΩ Just-In-Time Code Generation.

ѩ MATLAB ܼƸƫAiHHɧܡA] 6.5 e MATLAB b p-code ɡAn@ǮɶӨMwܼƪAAoNOݩ p-code g`}P@A|CtסCӦb JIT [tbͭOeA|C@C MATLAB {XiƫARAҧⴤhOGpGY@C{XwgQRLAYJPˤe@C{XAܼƪƫAM׳өMwRLe@C{X@ˡCھڳoӭhAunڭ̦bj餤ܼƳO@˪ƫAMסA JIT [tNiHjTaIJvC

Ū̩γ\|ݡGJMwgF JIT [tAڭ̬O_٭nϥΦVqƹBOH򥻪P_hpUG

  1. pGҥΪVqƹBëDڰuHŶɶvA٬OnɶqϥΡ]רOnɶqϥΤثO^A]oئVqƹB⤣ݭnB~ŶӼȦsTA]IJv| JIT [tnC]ШHUӽdҡC^
  2. pGҥΪVqƹBOڰuHŶɶvANgLJӴաA~MwO_ȱoϥΡC
  3. IƵc]Ҧp}x}κ׶WL 3 }C^AJIT [tä|নOA]boرpUA]nɶqϥΦVqƹBC

HUN|XXӦVqƹB⪺dҨӶi汴QAHKŪAѡuVqƹBvMuJIT [tvo̹ MATLAB IJv椬vTC]ѩoǽdҩM MATLAB KYA]ڭ̦bCӽdҳLX MATLAB AHѰѦҡC^

ڭ̨ϥΡu露v]Element by Element^x}kAӻpϥΡuݭnB~ŶvVqƹBӴ@ptסG

Example 1: 02-{XPO餧̨Τ/matMultiply01.mfprintf('MATLAB version = %s\n', version); ns = 10*(1:30); for j=1:length(ns) n = ns(j); a = rand(n); b = rand(n); % Ĥ@ؤkGfor-loop operation c1 = zeros(n); tic for p = 1:n for q = 1:n c1(p, q) = a(p, q)*b(p, q); end end time1(j)=toc; % ĤGؤkGvectorized operation tic c2 = a.*b; time2(j)=toc; end subplot(2,1,1); plot(ns, time1, 'v-', ns, time2, '^-'); grid on legend('time1 for for-loop code', 'time2 for vectorized code', 'location', 'NorthWest'); xlabel('n'); ylabel('second'); subplot(2,1,2); plot(ns, time1./time2, '.-'); grid on xlabel('n'); ylabel('time1/time2'); fprintf('isequal(c1, c2) = %g\n', isequal(c1, c2));MATLAB version = 8.1.0.604 (R2013a) isequal(c1, c2) = 1

bWzdҤAڭ̭pӯx}u露vnAbWϤAĤ@ӹϵeXjBMVqƹBҪ᪺ɶAĤGӹϫhOɶȡCѲĤGӹϥiHݥXA[tƤjb 5 HWC

pG for j骺hƼW[Apt׷|CA]ΦVqƹB⪺[tį|[ۡAUoӽdҡAڭ̨ϥΤ@몺x}kӶiҡG

Example 2: 02-{XPO餧̨Τ/matMultiply02.mfprintf('MATLAB version = %s\n', version); ns = 10*(1:20); for j=1:length(ns) n = ns(j); a = rand(n); b = rand(n); % Ĥ@ؤkGfor-loop operation c1 = zeros(n); tic for p = 1:n for q = 1:n for r = 1:n c1(p, q) = c1(p, q)+a(p, r)*b(r, q); end end end time1(j)=toc; % ĤGؤkGvectorized operation tic c2 = a*b; time2(j)=toc; end subplot(2,1,1); plot(ns, time1, 'v-', ns, time2, '^-'); grid on legend('time1 for for-loop code', 'time2 for vectorized code', 'location', 'NorthWest'); xlabel('n'); ylabel('second'); subplot(2,1,2); plot(ns, time1./time2, '.-'); grid on xlabel('n'); ylabel('time1/time2'); fprintf('isequal(c1, c2) = %g\n', isequal(c1, c2));MATLAB version = 8.1.0.604 (R2013a) isequal(c1, c2) = 1

ѤWzdҥiݥXAbnB~ŶeUAϥΦVqƹBNdҡ]j{X^Ħa[tƤQHWC

ۧڭ̨Ӭݥt~@VqƹBAo@dҳq`nB~OŶAwFuHŶɶvĪGCѩ JIT [t𪺧@ΡAuHŶɶvĪGiण|ۡA]ڭ̳q`ngLڴաA~obVqƫO_ڥ[t\C

bUoӽdҡAڭ̭np n թMƦC`MA䤤 n O@ӫܤjơAڭ̤OϥΰjΦVqƪ覡ӶipAܥ[tơApUG

Example 3: 02-{XPO餧̨Τ/hsum01.mfprintf('MATLAB version = %s\n', version); n = 100000000; % === Ĥ@ؤkGfor-loop operation tic total1 = 0; for i = 1:n total1 = total1+1/i; end time1 = toc; % === ĤGؤkGvectorized operation tic total2 = sum(1./(1:n)); time2 = toc; fprintf('time1 = %g, time2 = %g, speedup factor = %g\n', time1, time2, time1/time2);MATLAB version = 8.1.0.604 (R2013a) time1 = 0.842769, time2 = 0.612797, speedup factor = 1.37528

ѤWzdҥiAϥΦVqƪBAbtתTɡ]oiHӦҤP^AìOD`ۡCOإ[tƬOH n ܡAڭ̥iHeXYupUG

Example 4: 02-{XPO餧̨Τ/hsum02.mfprintf('MATLAB version = %s\n', version); ns = 1000*(1:1000); for i=1:length(ns) n=ns(i); % Ĥ@ؤkGfor-loop operation tic total1 = 0; for j = 1:n total1 = total1+1/j; end time1 = toc; % ĤGؤkGvectorized operation tic total2 = sum(1./(1:n)); time2 = toc; % p⭿ speedupFactor(i)=time1/time2; end plot(ns, speedupFactor, '.-'); grid on xlabel('n'); ylabel('time1/time2');MATLAB version = 8.1.0.604 (R2013a)

ѤWzuiHݥXA[tƨäO@²滼WλuALun n jA[tƪȳ|j@ANVqƹBboӽdҪT|[tĪGCƹWApGz{Oº骺ƭȹBAӥBSϥΥ for j while jAtױN|۷º C yg{XC

`NOAWzVqƪB⦡ھڦVq sequence ӨDMAO sequence ܼƥNnΰOŶA]oئVqƹB⪺iHOuHŶɶvAOpG n ӤjAyһݪŶWLOeqAɾN|NƼȦswСAyBtתCAӤϦӬݤXӦVqƹB⪺uIC

Hint
  • Yھڧڪ¹q]Pentium-450, 256 MB RAM^b MATLAB 6.1]䴩 JIT^ӶiաAWzӽdҪtʷ|[ۡAVqƵ{Xt׬ODVqƵ{X 40 kC
  • b MATLAB 6.5 AĥΤF JIT sĶ޳NA]uVqơvMuDVqơv{t׮twgܤpC

~AF JIT [to̤jįAڭ̪{X]nɶqtXAHUOXI`NƶG

  1. j餤ܼƺɶqϥί¶qC
  2. j餤ܼƺɶqO²檺ƫAAåHWLGDC
  3. j餺ҩIs禡ȭ MATLAB ب禡C

MVqƵ{XwgAO MATLAB IJvߤ@kAb`ڭ٬ONwVqƪBӶi满ADn۲IbG

  1. ݭnӶOB~O骺VqƵ{XA٬ODVqƪ{XӱoIJvC
  2. YŪ٦bϥ MATLAB 6.5 eAVqƪB٬OIJvDnC
  3. IƵc]Ҧp}x}κ׶WL 3 }C^AJIT įõLkoA٬OݭntXVqƪB~iH{X󦳮IJvC

@ MATLAB {]p̪{װCAiѨuVqƤBvϥμ_{רӬݥXCnmaBΦVqƪBAUCTnG

  1. nx}ޡ]Indexing^D`xAp~[HקKϥΰjC
  2. nMATLABiΪء]Built-in^OD`AѡAp~ɶqϥγoǧֳtOC
  3. nD{ѲMA~NƦϥΤثOYiѨMtL{C

HUڭ|@ǦVqƹB⪺²]o`^νdҡCҦpAw@Vq a Τ@x} xAYnN a C@ӤW x C@ӪAڭ̥iϥΤTؤkG

  1. H for jӧC
  2. Hت diag OӹF\AVqƹBAnB~ŶC
  3. H bsxfun OӹF\AVqƹBAnB~ŶC

dҦpUG

Example 6: 02-{XPO餧̨Τ/colMultiply02.mfprintf('MATLAB version = %s\n', version); n = 10000; x = rand(3, n); y1 = zeros(size(x)); a = 1:n; % === 1 ؤkGfor-loop operation tic for i = 1:n y1(:,i)=x(:,i)*a(i); end time1 = toc; % === 2 ؤkGvectorized operation which requires extra space tic y2 = x*diag(a); time2 = toc; % === 3 ؤkGbuiltin command for vectorized operation tic y3 = bsxfun(@times, x, a); time3 = toc; fprintf('time1 = %g sec, time2 = %g sec, time3 = %g sec\n', time1, time2, time3); fprintf('time1/time2 = %g\n', time1/time2); fprintf('time1/time3 = %g\n', time1/time3);MATLAB version = 8.1.0.604 (R2013a) time1 = 0.00156713 sec, time2 = 0.310063 sec, time3 = 0.00122577 sec time1/time2 = 0.00505423 time1/time3 = 1.27849

ѤWzdҥiHݥXG Ѧڭ̥iHo@²檺סGYnBtסAڭ̭nɶqϥΡunB~ŶثOvI

ڭ̨ӬݬݴXӡunB~ŶثOv[tĪGCĤ@ӫOO cellfunAiΨӹﲧ}CC@ӤiBCUoӽdҡAڭŪJ@ CMU o]]t12Uӭ^JΨo^AMpC@ӭ^yJסG

Example 7: 02-{XPO餧̨Τ/cellFun01.mfile='english.wpa'; [word, pa]=textread(file, '%s %s'); tic; len1=zeros(length(word),1); for i=1:length(word) len1(i)=length(word{i}); end time1=toc; tic; len2=cellfun(@length, word); time2=toc; fprintf('time1=%g sec, time2=%g sec, time1/time2=%g\n', time1, time2, time1/time2);time1=0.214358 sec, time2=0.0536109 sec, time1/time2=3.9984

ѦiHݥXAثO cellfun Tpt׸֡C]t@OO arrayfunAiH@}CC@ӤiBAŪ̦ۦѦҽuWAbAحzC^

LVqƪdҩMDAṶ̥̄iHѦҥDMѵC

pGzҥΪtkLkVqơAOpɶSܤ[AӥB MATLAB JIT ]WAzNiHҼ{UCסG

Ӹ`Шѡuε{v`C
MATLAB{]pGig