title: One step reduction in MATLAB increases the execution time
body: <p>I am doing a comparison and performance test of 3 methods to get the closest index of what i click in the <code>ginput()</code> the first method takes distance from each click and finds the nearest index in the next step the second one is doing the same but through a <code>for</code> loop and 3rd is the exact same copy of the first one but reduction of one step </p>
  
  <pre><code>    ax = subplot(1,1,1)
      plot(timestamps,datavalue)
      hzoom = zoom(ax);                                               
      hzoom.Motion = 'horizontal'; 
      [x,  ~] = ginput(2);
  %1)
      tic;
      tmp = abs(bsxfun(@minus,x,datenum(timestamps).'));
      [~, idx1] = min(tmp,[],2);
      toc;
  %2)
      tic;
             for r = 1:length(x)                                               
                 val = x(r);
                 tmp = abs(datenum(datenum(timestamps - val)));                                
                 [~, idx2] = min(tmp);                                          
                 closest_indx(r) = idx2;                                        
             end
             toc;
  %3)
  
      tic;
  
      [~, idx3] = min(abs(bsxfun(@minus,x,datenum(timestamps).')),[],2);
      toc;
  </code></pre>
  
  <p>Now when I look at the results </p>
  
  <pre><code>test1)
  Elapsed time is 0.009182 seconds.
  Elapsed time is 0.019211 seconds.
  Elapsed time is 0.011261 seconds.
  test2)
  Elapsed time is 0.012625 seconds.
  Elapsed time is 0.022681 seconds.
  Elapsed time is 0.017999 seconds.
  test3)
  Elapsed time is 0.013053 seconds.
  Elapsed time is 0.020170 seconds.
  Elapsed time is 0.015248 seconds.
  test4)
  Elapsed time is 0.011613 seconds.
  Elapsed time is 0.018644 seconds.
  Elapsed time is 0.015952 seconds.
  </code></pre>
  
  <p>It takes less time for the first method even though it has one more step of taking all the values and placing in into a '<code>tmp</code>' matrix. Does anyone have a good explaination for this ?</p>