From: James Tursa on
"Max " <gm.pantagruel(a)gmail.com> wrote in message <i0ds1o$7p5$1(a)fred.mathworks.com>...
> > 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.

One important difference between calling m-files and calling mex-files is the following:

- For m-files, MATLAB creates a temporary shared data copy of the argument(s) and passes the addresses of these to the m-function. So there is some overhead in creating the shared data copies.

- For mex files, MATLAB passes the addresses of the original argument(s) to the mex-function. There is *no* extra overhead in creating shared data copies involved.

I suspect that it is simply a slight timing difference between creating & clearing these temporary shared data copies that is showing up in your timing tests. e.g., for these two tests:

clear all
tic
b = somefunction(some arguments);
toc

vs this

b = something;
tic
b = somefunction(some arguments);
toc

The timings will be different. That is because the 2nd example has to clear the pre-existing b before it can assign it a new value. That extra overhead will show up in the tic-toc timing.

Since the actual time associated with this overhead should always be quite small, I probably wouldn't worry about it in the grand scheme of things. Other things going on inside your class constructors & member functions will likely swamp that tiny amount as far as timing goes. I would advise spending your time optimizing other parts of your code.

James Tursa