In the process of analyzing how fast a trivial loop could be, I encountered this strange phenomenon.
Doing nothing with a variable is much slower than doing something with it.
Of course this is not a real problem as you won't often feel the urge to write code that does nothing, but this surprised me so I wonder if anyone understands what is happening and whether this could be a problem in real situations.
Here is what I found:
tic,for t= 1:1e6, x=x; end,toc %This runs very fast, about 0.07 sec
y=x; tic,for t= 1:1e6, y=x; end,toc %This runs fast, about 0.11 sec
tic,for t= 1:1e6, x; end,toc %This takes over half a second?!
I tried adding a trivial calculation in the loop to ensure the loop would not be optimized away but this did not change results.
To summarize, my question is:
What is happening and should I ever worry about it?
According to the documentation and this MATLAB answer, functions are generally faster than scripts.
Direct link to this comment. For the example Tom gave, however, cellfun with string is much faster than a for loop (in answer to the original question). Elapsed time is 0.755874 seconds. Elapsed time is 0.913470 seconds.
Functions Are Faster Than Scripts Every time a script is used in MATLAB, it is loaded into memory and evaluated one line at a time. Functions, on the other hand, are compiled into pseudo-code and loaded into memory once. Therefore, additional calls to the function are faster.
The JIT accelerator is a moving target and trying to guess what it accelerates is nearly impossible. On my machine 64 bit Linux R2013a x=x
and y=x
take the same amount of time, but x
is much slower. If I turn the JIT accelerator off feature accel off
then x=x
and y=x
take more time, but x
stays the same. In fact with the accelerator off, x=x
takes the same time as x
(y=x
is a little slower). This suggests that the JIT accelerator doesn't work on x
. As to why TMW chose not to accelerate x
, your guess is as good as mine.
I believe that you ran your code within a script or in the command line. If you run it within a function you will see that all 3 variants take almost exactly the same amount of time. In the command line, Matlab cannot employ all of its available optimizations and so the first 2 variants are optimized while the third is not. This is not very concerning, since Matlab code is typically run within functions, where the optimizations' effectiveness is maximized.
When comparing different execution paths for performance, you should always test within a function, as tempting and easy as it is to test in the command prompt.
p.s. - In this particular case, it is not the JIT that is at fault but rather the interpreter's other (non-JIT) set of accelerations. The behavior vis-a-vis command-line vs. function is the same as with JIT, namely that many optimizations available within functions are unavailable in the CL. You can see this by seeing that feature jit off
makes no real difference regarding the timing variability (it makes variants #1+#2 slower, but they are still faster than variant #3) - but when you run feature accel off
all the differences between the variants are eliminated and all of them run at the slow speed of variant #3 (0.5 secs in the CL, 0.25 secs in a function). JIT is a apparently subset of accel, so turning accel off also turns off JIT (but not vise versa).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With