why arrayfun does NOT improve my struct array operation performance

Posted by HaveF on Stack Overflow See other posts from Stack Overflow or by HaveF
Published on 2012-06-23T14:00:44Z Indexed on 2012/06/23 15:16 UTC
Read the original article Hit count: 212

Filed under:
|

here is the input data:

  % @param Landmarks:
  %           Landmarks should be 1*m struct. 
  %           m is the number of training set.
  %           Landmark(i).data is a n*2 matrix

old function:

  function Landmarks=CenterOfGravity(Landmarks)
  % align center of gravity

  for i=1 : length(Landmarks)
      Landmarks(i).data=Landmarks(i).data - ones(size(Landmarks(i).data,1),1)...
          *mean(Landmarks(i).data);
  end
  end

new function which use arrayfun:

  function [Landmarks] = center_to_gravity(Landmarks)
  Landmarks = arrayfun(@(struct_data)...
                          struct('data', struct_data.data - repmat(mean(struct_data.data), [size(struct_data.data, 1), 1]))...
                                              ,Landmarks);
  end %function center_to_gravity

when using profiler, I find the usage of time is NOT what I expected:

  Function          Total Time    Self Time*
  CenterOfGravity     0.011s      0.004 s
  center_to_gravity   0.029s      0.001 s

Can someone tell me why?

BTW...I can't add "arrayfun" as a new tag for my reputation.

© Stack Overflow or respective owner

Related posts about matlab

Related posts about profiler