Thread Subject: mexCallMATLAB is slow

Subject: mexCallMATLAB is slow

From: Nikolai Yu. Zolotykh

Date: 14 Nov, 2006 08:30:46

Message: 1 of 3

mexCallMATLAB is slower.

Let
A=zeros(1000);

I want to do an assignment

A(1,1) = 1

In MATLAB environment it requires 0.000027 seconds:

tic;A(1,1)=1;toc;
Elapsed time is 0.000027 seconds.

The following mexFucntion does the same:

/* myassignment.c */
#include "mex.h"
void mexFunction(int nOut, mxArray *pOut[],
int nIn, const mxArray *pIn[])
{
  mexCallMATLAB(1, pOut, 3, pIn, "subsasgn");
}

mex myassignment.c
A=zeros(1000);
S.type = '()';
S.subs = {1,1};
A=myassignment(A, S, 1);
A(1,1)

ans =

     1

But now the time requiered is 0.020915 seconds:

tic;A=myassignment(A, S, 1);toc;
Elapsed time is 0.020915 seconds.

It is too slow!

I suppose in mexCallMATLAB MATLAB copies matrices and does not make
the subscript assignment in-place!

Any suggestions are welcome.

Subject: mexCallMATLAB is slow

From: Titus Edelhofer

Date: 15 Nov, 2006 10:05:07

Message: 2 of 3

Hi,
in the mex file you shouldn't use mexCallMATLAB, but
just assign values to the array, something like

/* copy the array */
pOut[0] = mxDuplicateArray(pIn[0]);
/* get the pointer */
double *ptr = mxGetPr(pOut[0]);
/* do the assignment: */
ptr[0] = 1;

Titus

"Nikolai Yu. Zolotykh" <zny@uic.nnov.ru> schrieb im Newsbeitrag
news:ef45ef9.-1@webcrossing.raydaftYaTP...
> mexCallMATLAB is slower.
>
> Let
> A=zeros(1000);
>
> I want to do an assignment
>
> A(1,1) = 1
>
> In MATLAB environment it requires 0.000027 seconds:
>
> tic;A(1,1)=1;toc;
> Elapsed time is 0.000027 seconds.
>
> The following mexFucntion does the same:
>
> /* myassignment.c */
> #include "mex.h"
> void mexFunction(int nOut, mxArray *pOut[],
> int nIn, const mxArray *pIn[])
> {
> mexCallMATLAB(1, pOut, 3, pIn, "subsasgn");
> }
>
> mex myassignment.c
> A=zeros(1000);
> S.type = '()';
> S.subs = {1,1};
> A=myassignment(A, S, 1);
> A(1,1)
>
> ans =
>
> 1
>
> But now the time requiered is 0.020915 seconds:
>
> tic;A=myassignment(A, S, 1);toc;
> Elapsed time is 0.020915 seconds.
>
> It is too slow!
>
> I suppose in mexCallMATLAB MATLAB copies matrices and does not make
> the subscript assignment in-place!
>
> Any suggestions are welcome.

Subject: mexCallMATLAB is slow

From: Zolotykh

Date: 16 Nov, 2006 02:20:31

Message: 3 of 3

> in the mex file you shouldn't use mexCallMATLAB, but
> just assign values to the array, something like

Yes.
But my subscription can be very complicated and I don't want to treat
this by myself.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com