%{ test_JIT.m Stephen Becker, October 2008 Matlab's JIT (Just-In-Time) accelerator will compile some code just before the code is run, and this often results in dramatic speed savings, especially if the code isn't already vectorized. The accelerator is undocumented, but we can see its effects with a simple example. For the code below, uncomment the lines that say "feature accel ..." to turn the accelerator on and off. The code below was run with the accelerator on, and we show how the effects of "line A" and "line B" can ruin the benefits of the accelerator. Also, we see differences in the various versions of Matlab. R2006 didn't accelerate code in "cell mode", but R2008 does. Neither version accelerates code from the command line -- basically, the JIT needs to be able to parse "everything at once" (like in a traditional C compiler). %} % feature accel off N = 1000000; a = randn(N,1); b = randn(N,1); tic s = 0; for k = 1:N s = s + a(k)*b(k); end toc % The timing is done, so these lines don't affect the recorded timing, but % they can affect whether the JIT is invoked. % k = 19; % line "A" % k = [4,5]; % line "B" % feature accel on %{ Results: (the discrepancies were very reproducible) "with line A" means that line "A" was NOT commented out, ... "m-file" means that this file was run from the command line, as opposed to in cell-mode. all tests on a Pentium 4 3.00 GHz processor with HyperThreading **** Windows **** R2006a: 2.0394 (cell mode) -- line "A" and "B" have no effect It appears the JIT is not called. 0.0190 (m-file) -- the JIT is called 0.0422 (m-file, with "line A") -- ??? 2.447 (m-file, with "line B") The JIT is not called R2008a: 0.0430 (cell mode) 0.0427 (cell mode, with line "A") 0.0430 (cell mode, with line "B") 0.0146 (m-file) 0.0435 (m-file, with "line A") 1.8710 (m-file, with "line B") In R2008, the JIT is invoked in cell mode, but there are still bizarre differences between calling a function in cell mode and from the command line. **** Redhat Linux **** R2006a: 1.73 (cell mode -- lines "A" and "B" have no effect) JIT is not called 0.011 (m-file) 0.038 (m-file, with "line A") 2.90 (m-file, with "line B") with accel off: 1.76 (cell mode -- lines "A" and "B" have no effect) 2.20 (m-file -- lines "A" and "B" have no effect) **** Debian Linux **** R2007a: 1.80 (cell mode -- lines "A" and "B" have no effect) JIT is not called 0.011 (m-file) 0.038 (m-file, with "line A") 1.90 (m-file, with "line B") a slight improvement over R2006a with accel off: 1.79 (cell mode -- lines "A" and "B" have no effect) 2.40 (m-file -- lines "A" and "B" have no effect) It seems that cell mode can be faster than calling the script sometimes, perhaps due to overhead in calling a script?? %}