MATLAB: Does the GUI execute an order of magnitude faster when I turn on the profiler

accelacceleratorjitMATLABprofileprofilerspeed

I have created a GUI which when run, executes in 9 seconds consistently. When the profiler is turned on, the GUI executes in 1.2 seconds consistently. When the profiler window is opened, the GUI executes in 0.7 seconds consistently.
% Run your GUI
% Execute the callback in your GUI (may be by selecting/clicking a UICONTROL)
profile on % Code executes faster
profile viewer % Code execution speed improves further

Best Answer

The behavior observed is not a bug in MATLAB.
It occurs because the JIT is unable to completely compile the code the first time the callback is executed. This happens when a variable in the handles structure is not set before it is used in a callback function. For this reason, the JIT accelerator is unable to completely compile this code and hence the slower execution. Doing a JIT on/off, clear all, or profiler on/off after running the code once forces the JIT to recompile the callback function when the variable has already been set and becomes visible to the JIT.
To resolve the issue, set the variable in the handles structure before using it in the callback function.