File Exchange

image thumbnail

Performance in object-oriented matlab code

version 1.2 (22.1 KB) by

A very simple example to demonstrate an observation of mine regarding the use of classes in matlab.

2 Downloads

Updated

View License

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
called.

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!

Comments and Ratings (8)

Arabarra

   
Looks like the OOP system is performing better. Just ran this code on R2016a. Handle classes perform slower but comparably to nested functions.

 core: 0.0329
      c1: 1.3159
      c2: 0.3075
    c1_h: 0.1252
    c2_h: 0.1084
       f: 0.0979

As reference, in the same computer, the same code gave me for R2015a:
    core: 0.0469
      c1: 1.8255
      c2: 0.9009
    c1_h: 0.4718
    c2_h: 0.3569
       f: 0.1072

R2016a release notes did mention a speedup in the OOP system, looks like they are indeed putting some effort on this issue.

Eric Sampson

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.

To Josh:
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!

Josh

Josh (view profile)

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.

per isakson

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.

My result:

>> PerfTest
time =
    core: 0.0627
      c1: 6.1896
      c2: 1.3763
       f: 0.2748
     ch1: 0.6555
     ch2: 0.5216
     
The handle-classes perform better than the value-classes.

Oliver Woodford

Interesting. It would be useful to see how a handle class performs in comparison.

Bruno Luong

Bruno Luong (view profile)

At last, someone clearly points out the performance issue of MATLAB OOP.

Matthew

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.

Updates

1.2

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.

MATLAB Release
MATLAB 8.1 (R2013a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

ClassPerformance/