Code covered by the BSD License  

Highlights from
Performance in object-oriented matlab code

4.66667

4.7 | 3 ratings Rate this file 15 Downloads (last 30 days) File Size: 22.1 KB File ID: #41349
image thumbnail

Performance in object-oriented matlab code

by

 

16 Apr 2013 (Updated )

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

| Watch this File

File Information
Description

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!

Required Products MATLAB
MATLAB release MATLAB 8.1 (R2013a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
18 Apr 2013 Eric Sampson  
18 Apr 2013 Dimitrios Korkinof

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!

18 Apr 2013 Josh

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.

17 Apr 2013 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.

17 Apr 2013 Oliver Woodford

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

17 Apr 2013 Bruno Luong

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

16 Apr 2013 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
18 Apr 2013

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.

Contact us