This is a very simple demo made solely for the purpose of demonstrating the high overhead that seems to be induced when using object-oriented programming in Matlab. The problem is especially intense when having classes containing a lot of data and one or more functions in the class need to be frequently
I have implemented 2 distinct scenarios using classes. According to the first (C1), the class contains a large array, let's say 200x200. A function member of the class putting a random number in one element at a time. In the second scenario (C2), the class member function is not modifying the large array, but puts random value in another variable member of the class.
In both cases we call the class member functions 40.000 times and time the code. We also time the actual duration of the core operation without the use of functions or classes, in order to measure the overhead induced in each case.
Fortunately, there is an alternative to using classes in Matlab. The construction is a form of class consisted of nested functions, the handlers to which are made externally available. Of course, inheritance is not available in this case, however this construction is similar enough.
Is there something I am missing here? Is this normal or expected? If not, could something have been done to boost performance?
I am looking forward to your comments!
Looks like the OOP system is performing better. Just ran this code on R2016a. Handle classes perform slower but comparably to nested functions.
As reference, in the same computer, the same code gave me for R2015a:
R2016a release notes did mention a speedup in the OOP system, looks like they are indeed putting some effort on this issue.
Thank you guys! I did not know about the handler classes!
I've tested it and it runs faster indeed, but again slower than nested functions. I will update the submission as soon as possible.
Yes, you are right, the functions do not modify the objects! This was a change I made just before submission, thinking that it would run faster this way. I will update it. Thanks for the comment!
There are two types of classes in MATLAB: value classes and handle classes.
When you call a member function of a value class, MATLAB copies the entire object into the member function space. This is why your code takes so long to run; each time you edit one element in the 400x400 array, MATLAB has to make a copy of the entire array.
If you use a handle class (put "< handle" at the end of the classdef line) then MATLAB only passes a pointer to the class to each member function. Thus, handle classes in MATLAB behave more like classes you're probably used to in other languages, and your code should run MUCH faster if you use a handle class.
Incidentally, you need to return a value from object class methods in order to modify the original object. Your modify_one function doesn't actually modify the c1 and c2 objects you define in PerfTest.m. If you were to examine the value of large_mat after the test code competes, it will still be all zeros.
I modified and ran your code with R2012a 64bit on Windows 7.
Firstly, I changed the code to return the object for the value-classes, e.g. c1 = modify_one(c1,i,j); That didn't change much.
Secondly, I added two handle-classes, Ch1 and Ch2, based on the value-classes.
The handle-classes perform better than the value-classes.
Interesting. It would be useful to see how a handle class performs in comparison.
At last, someone clearly points out the performance issue of MATLAB OOP.
No your not missing anything. Matlab is very slow when working with classes. There are numerous examples on the web, but very little on the FEX; I welcome your submission.
Taking into consideration the accurate comments below, I have added two examples with "handle classes" and corrected a small mistake that resulted in the class member data not actually being updated.
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.