performance - Matlab scalar operation takes longer than array operation -


in using profiler speed code, noticed scalar operations on single array element taking longer vectorized operations on entire array. obviously, not 1 expect, since there 1 operation taking place when working array element, many operations (albeit vectorized) when operating vectorizedly on array.

the context in saw bit complicated, scalar operation not being done on same nested object array. however, able replicate oddity script:

%%%%%%%%%%%%% %%  tst1.m %%%%%%%%%%%%%  % generate random data ix=1:5; iy=1:5    x(ix).y(iy).z=rand(1,10); end; end ntest=1e7;  disp('script tst#1a: operation on 1 array element:') tic i=1:ntest    a=0.5>x(3).y(3).z(1); end % toc  % clear  disp('script tst#1b: vectorized operation on entire array:') tic i=1:ntest    a=0.5>x(3).y(3).z; end % toc 

script tst#1a above single array element operation while script tst#1b vectorized operation on entire array. results are:

script tst#1a: operation on 1 array element: elapsed time 6.260495 seconds. script tst#1b: vectorized operation on entire array: elapsed time 4.491822 seconds. 

as can seen scalar operation takes longer. able surmise reason counterintuitive observation? perhaps silly in test code?

in assembling above test, found if cleared left-hand-side variable, such in statement commented out above, scalar operation sped factor of 2. don't know why, regardless of reason, found odder scalar operation test code sped though clearing occurs after scalar operation test code. here same m-file clear command uncommented:

%%%%%%%%%%%%% %%  tst2.m %%%%%%%%%%%%%  % generate random data ix=1:5; iy=1:5    x(ix).y(iy).z=rand(1,10); end; end ntest=1e7;  disp('script tst#2a: operation on 1 array element:') tic i=1:ntest    a=0.5>x(3).y(3).z(1); end % toc  disp('clearing a'); clear  disp('script tst#2b: vectorized operation on entire array:') tic i=1:ntest    a=0.5>x(3).y(3).z; end % toc 

here result, showing inexplicable speedup of preceding scalar operation test code (in comparison results tst1.m):

script tst#2a: operation on 1 array element: elapsed time 3.371326 seconds. clearing script tst#2b: vectorized operation on entire array: elapsed time 4.463924 seconds. 

neither of these tests reflective of situation, uses class methods instead of scripts. recall reading on forum that, compared scripts, functions , methods provide more opportunities compiler optimizations. in order clue whether might explain relative slowness of scalar operations , counterintuitive speedup due post hoc clear, put above 2 test scripts class methods:

%%%%%%%%%%%%%% %%  ctest.m %%%%%%%%%%%%%% classdef ctest < handle methods     function tst1(o)        % generate random data       ix=1:5; iy=1:5          x(ix).y(iy).z=rand(1,10);       end; end       ntest=1e7;        disp('method tst#1a: operation on 1 array element:')       tic       i=1:ntest          a=0.5>x(3).y(3).z(1);       end %       toc        % clear       disp('method tst#1b: vectorized operation on entire array:')       tic       i=1:ntest          a=0.5>x(3).y(3).z;       end %       toc     end % function tst1     function tst2(o)        % generate random data       ix=1:5; iy=1:5          x(ix).y(iy).z=rand(1,10);       end; end       ntest=1e7;        disp('method tst#2a: operation on 1 array element:')       tic       i=1:ntest          a=0.5>x(3).y(3).z(1);       end %       toc        disp('clearing a');       clear        disp('method tst#2b: vectorized operation on entire array:')       tic       i=1:ntest          a=0.5>x(3).y(3).z;       end %       toc     end % function tst2  end % method end % classdef 

i compare execution of above m-files using following "testbench" script:

%%%%%%%%%%% %%  go.m %%%%%%%%%%% clc c = ctest;  tst1 disp(' ') tst2  fprintf('\n\n')  c.tst1 disp(' ') c.tst2 

the combined results are:

script tst#1a: operation on 1 array element: elapsed time 5.888381 seconds. script tst#1b: vectorized operation on entire array: elapsed time 4.636491 seconds.  script tst#2a: operation on 1 array element: elapsed time 3.435526 seconds. clearing script tst#2b: vectorized operation on entire array: elapsed time 4.531256 seconds.   method tst#1a: operation on 1 array element: elapsed time 5.732293 seconds. method tst#1b: vectorized operation on entire array: elapsed time 4.550085 seconds.  method tst#2a: operation on 1 array element: elapsed time 3.266772 seconds. clearing method tst#2b: vectorized operation on entire array: elapsed time 4.664736 seconds. 

out of 4 blocks of output text, 1st 2 blocks re-run of 2 sript tests above, while last 2 blocks of output execute same code, class methods. results similar, inexplicable slowness of scalar operation, , counterintuitive speedup due post hoc clear command, not seem affected compilation difference between scripts , class methods.

in summary,

  1. the scalar operation on array element seems inexplicably run slower array operation. perhaps there kind of speed penalty associated extracting single element array, of unaware.

  2. a post-hoc clear inexplicably speeds scalar operation faster array operation. 1 expect regardless of presence of clear command.

  3. these observations not seem affected compilation differences between scripts , class methods.

if can shed light on inner workings might lead above observations, perhaps use insight rid of slowness of scalar operations on individual array elements in class methods.

afternote: observation#1 seen without nesting array in layers of structure arrays:

>> clear all; x=rand(1,10); tic; i=1:1e7; a=0.5>x(1); end; toc    elapsed time 0.092028 seconds.  >> clear all; x=rand(1,10); tic; i=1:1e7; a=0.5>x; end; toc    elapsed time 1.344769 seconds. 

this using matlab version 8.5.0.197613 (r2015a) on 3ghz laptop running 64-bit windows 7 8gb ram , not else running consume memory. matlab using 550gb , internet explorer using 240gb.

alexander kemp's answer seems explanation, based on info date. indexing array access individual elements seems come significant time overhead. not scalar operation per se takes longer vectorized operation; extraction of element array scalar operation causes speed penalty.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -