From: Max on
Hello,
I'm trying to improve the performance of Matlab OOP code by converting object's member functions into mex files. It was possible before by using "mcc -x funcname", but starting from r2010a (or maybe even earlier) it says:

"Error: -x is no longer supported. The MATLAB Compiler no longer generates
MEX files because there is no longer any performance advantage to doing so: the
MATLAB JIT accelerates M-files by default."

Should I take it for granted OR there is a slight chance that if I find somewhere some old version of matlab software and try to convert my m-files into mex-files I could get a slight performance increase - I don't expect a big one.

PS
I'm using the "old" object model where each class must reside in its own @folder.
My performance benchmark is the time spent on object initialization.
tic;
b = myclass(a); %thinknig about converting constructor to mex
toc;
From: James Tursa on
"Max " <gm.pantagruel(a)gmail.com> wrote in message <i03pk1$e28$1(a)fred.mathworks.com>...
> Hello,
> I'm trying to improve the performance of Matlab OOP code by converting object's member functions into mex files. It was possible before by using "mcc -x funcname", but starting from r2010a (or maybe even earlier) it says:
>
> "Error: -x is no longer supported. The MATLAB Compiler no longer generates
> MEX files because there is no longer any performance advantage to doing so: the
> MATLAB JIT accelerates M-files by default."
>
> Should I take it for granted OR there is a slight chance that if I find somewhere some old version of matlab software and try to convert my m-files into mex-files I could get a slight performance increase - I don't expect a big one.
>
> PS
> I'm using the "old" object model where each class must reside in its own @folder.
> My performance benchmark is the time spent on object initialization.
> tic;
> b = myclass(a); %thinknig about converting constructor to mex
> toc;

Tremendous speed gains with mex files can still be achieved, but only in certain circumstances and maybe only if you rewrite the m-file by hand in C rather than letting mcc generate it. For example, MATLAB is typically a poor performer when dealing with array slices because it has to copy the data into a new memory block before working with it. This can be avoided in a mex routine. For your case, it will depend on what, exactly, the m-file is doing.

James Tursa
From: Max on
> Tremendous speed gains with mex files can still be achieved, but only in certain >circumstances and maybe only if you rewrite the m-file by hand in C rather than letting >mcc generate it. For example, MATLAB is typically a poor performer when dealing with >array slices because it has to copy the data into a new memory block before working with >it. This can be avoided in a mex routine. For your case, it will depend on what, exactly, >the m-file is doing.
>
> James Tursa

Thank you for your reply James. Let me give you some more details on what I'm trying to accomplish.

I'm trying to improve performance of the following code:
function this = myclass(h_var)
this = struct('handle', []);
this = class(this,'myclass');
end

it is a very simple class whish has only constructor. So I'm wondering what else can be done to further improve performance of object creation: b=myclass(a). I was thinking about compiling this constructor into mex-file since I've seen a class with compiled constructor which definitely capable of more that the code above and yet it takes less time to execute than the above code. There must be some performance trick. Unfortunately I could not check if compiling to mex will give anything in terms of performance since in matlab r2010a it is no longer possible to use "mcc -x". So I appreciate very much if anybody with older version of matlab could do it for me and report the result here.
Basically all you need to do is
1) create a folder with the name @myclass
2 ) inside that folder put a function myclass.m with the code listed above.
3) run tic;b=myclass(a);toc;
4) then execude "mcc -x myclass" to get myclass.mexNN and
5) repeat performance test again.
From: James Tursa on
"Max " <gm.pantagruel(a)gmail.com> wrote in message <i04jmc$fvq$1(a)fred.mathworks.com>...
>
> I'm trying to improve performance of the following code:
> function this = myclass(h_var)
> this = struct('handle', []);
> this = class(this,'myclass');
> end
>
> it is a very simple class whish has only constructor. So I'm wondering what else can be done to further improve performance of object creation: b=myclass(a). I was thinking about compiling this constructor into mex-file since I've seen a class with compiled constructor which definitely capable of more that the code above and yet it takes less time to execute than the above code. There must be some performance trick. Unfortunately I could not check if compiling to mex will give anything in terms of performance since in matlab r2010a it is no longer possible to use "mcc -x". So I appreciate very much if anybody with older version of matlab could do it for me and report the result here.
> Basically all you need to do is
> 1) create a folder with the name @myclass
> 2 ) inside that folder put a function myclass.m with the code listed above.
> 3) run tic;b=myclass(a);toc;
> 4) then execude "mcc -x myclass" to get myclass.mexNN and
> 5) repeat performance test again.

I can't run the requested test since the earliest version I have is R2006b and mcc -x isn't supported. However, I don't see anything in your constructor that would lead me to believe that a compiled version would run any faster. All you are doing is creating a structure and then tagging that structure with a class name ... neither of these activities is going to run any faster in a mex routine. And the m-file version runs so fast (0.000074 sec) that any tic-toc timing is pointless.

James Tursa
From: Max on
> I can't run the requested test since the earliest version I have is R2006b and mcc -x isn't >supported. However, I don't see anything in your constructor that would lead me to >believe that a compiled version would run any faster. All you are doing is creating a >structure and then tagging that structure with a class name ... neither of these activities >is going to run any faster in a mex routine. And the m-file version runs so fast (0.000074 >sec) that any tic-toc timing is pointless.
>
> James Tursa

Thank you James, you made me look into it again and realize that I forget to mention one important thing. I was getting 0.001505 sec just because I had 'clear all' statement before 'tic', so the actual code was:

clear all;
a=rand(1,10);
tic;
b = myclass(a);
toc;

And if you add 'clear all' you'll probably also have something around 0.001 sec. Very strange behavior. Even though it is still a complete mystery to me I discovered that by changing from 'clear all' to 'clear variables' I could get timings similar to yours ( around 0.00007 sec).

And now everything makes more sense. I started from the simple test:

tic;my_empty_class(a);toc;
tic;some_mex_class(a);toc;

and was puzzled after getting:

0.001505 sec - my 'empty' class
0.000122 sec - external mex class from 3rd party commercial library

The fact that my empty class was more that 10 times slower made me think that by converting to mex I could gain performance improvement. But now I believe that mex files treat 'clear all' statement somewhat differently and that is what makes such a big difference in timings between plain .m file and its .mex version in my case.