title: Is MATLAB OOP slow or am I doing something wrong?
body: <p>I'm experimenting with <a href="http://en.wikipedia.org/wiki/MATLAB" rel="noreferrer">MATLAB</a> <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" rel="noreferrer">OOP</a>, as a start I mimicked my C++'s Logger classes and I'm putting all my string helper functions in a String class, thinking it would be great to be able to do things like <code>a + b</code>, <code>a == b</code>, <code>a.find( b )</code> instead
  of <code>strcat( a b )</code>, <code>strcmp( a, b )</code>, retrieve first element of <code>strfind( a, b )</code>, etc.</p>
  <p><strong>The problem: slowdown</strong></p>
  <p>I put the above things to use and immediately noticed a <em>drastic</em> slowdown. Am I doing it wrong (which is certainly possible as I have rather limited MATLAB experience), or does MATLAB's OOP just introduce a lot of overhead?</p>
  <p><strong>My test case</strong></p>
  <p>Here's the simple test I did for string, basically just appending a string and removing the appended part again:</p>
  <blockquote>
  <p>Note: Don't actually write a String class like this in real code! Matlab has a native <code>string</code> array type now, and you should use that instead.</p>
  </blockquote>
  <pre><code>classdef String &lt; handle
    ....
    properties
      stringobj = '';
    end
    function o = plus( o, b )
      o.stringobj = [ o.stringobj b ];
    end
    function n = Length( o )
      n = length( o.stringobj );
    end
    function o = SetLength( o, n )
      o.stringobj = o.stringobj( 1 : n );
    end
  end
  
  function atest( a, b ) %plain functions
    n = length( a );
    a = [ a b ];
    a = a( 1 : n );
  
  function btest( a, b ) %OOP
    n = a.Length();
    a = a + b;
    a.SetLength( n );
  
  function RunProfilerLoop( nLoop, fun, varargin )
    profile on;
    for i = 1 : nLoop
      fun( varargin{ : } );
    end
    profile off;
    profile report;
  
  a = 'test';
  aString = String( 'test' );
  RunProfilerLoop( 1000, @(x,y)atest(x,y), a, 'appendme' );
  RunProfilerLoop( 1000, @(x,y)btest(x,y), aString, 'appendme' );
  </code></pre>
  <p><strong>The results</strong></p>
  <p>Total time in seconds, for 1000 iterations:</p>
  <blockquote>
  <p>btest 0.550 (with String.SetLength 0.138, String.plus 0.065, String.Length 0.057)</p>
  <p>atest 0.015</p>
  </blockquote>
  <p>Results for the logger system are likewise: 0.1 seconds for 1000 calls
  to <code>frpintf( 1, 'test\n' )</code>, 7 (!) seconds for 1000 calls to my system when using the String class internally (OK, it has a lot more logic in it, but to compare with C++: the overhead of my system that uses <code>std::string( &quot;blah&quot; )</code> and <code>std::cout</code> at the output side vs plain <code>std::cout &lt;&lt; &quot;blah&quot;</code> is on the order of 1 millisecond.)</p>
  <p><strong>Is it just overhead when looking up class/package functions?</strong></p>
  <p>Since MATLAB is interpreted, it has to look up the definition of a function/object at run time. So I was wondering that maybe much more overhead is involved in looking up class or package function vs functions that are in the path. I tried to test this, and it just gets stranger. To rule out the influence of classes/objects, I compared calling a function in the path vs a function in a package:</p>
  <pre><code>function n = atest( x, y )
    n = ctest( x, y ); % ctest is in matlab path
  
  function n = btest( x, y )
    n = util.ctest( x, y ); % ctest is in +util directory, parent directory is in path
  </code></pre>
  <p>Results, gathered same way as above:</p>
  <blockquote>
  <p>atest 0.004 sec, 0.001 sec in ctest</p>
  <p>btest 0.060 sec, 0.014 sec in util.ctest</p>
  </blockquote>
  <p>So, is all this overhead just coming from MATLAB spending time looking up definitions for its OOP implementation, whereas this overhead is not there for functions that are directly in the path?</p>