Is MATLAB OOP slow or am I doing something wrong?
I'm experimenting with MATLAB OOP, 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 a + b
, a == b
, a.find( b )
instead
of strcat( a b )
, strcmp( a, b )
, retrieve first element of strfind( a, b )
, etc.
The problem: slowdown
I put the above things to use and immediately noticed a drastic 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?
My test case
Here's the simple test I did for string, basically just appending a string and removing the appended part again:
Note: Don't actually write a String class like this in real code! Matlab has a native
string
array type now, and you should use that instead.
classdef String < 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' );
The results
Total time in seconds, for 1000 iterations:
btest 0.550 (with String.SetLength 0.138, String.plus 0.065, String.Length 0.057)
atest 0.015
Results for the logger system are likewise: 0.1 seconds for 1000 calls
to frpintf( 1, 'test
' )
, 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 std::string( "blah" )
and std::cout
at the output side vs plain std::cout << "blah"
is on the order of 1 millisecond.)
Is it just overhead when looking up class/package functions?
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:
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
Results, gathered same way as above:
atest 0.004 sec, 0.001 sec in ctest
btest 0.060 sec, 0.014 sec in util.ctest
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?