Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MEX files

Subject: MEX files

From: John Smith

Date: 13 Jul, 2011 21:05:31

Message: 1 of 6

For those that have used mex, (for "for" loops possibly), can anyone tell me if there is any noticeable difference in speed between writing loops in C or C++?

I wrote a simple "for" loop to calculate sines in C and was surprised to find that it wasn't much faster than MATLAB.

Subject: MEX files

From: James Tursa

Date: 13 Jul, 2011 21:30:27

Message: 2 of 6

"John Smith" <johnps@gmail.com> wrote in message <ivl1aq$mo4$1@newscl01ah.mathworks.com>...
> For those that have used mex, (for "for" loops possibly), can anyone tell me if there is any noticeable difference in speed between writing loops in C or C++?
>
> I wrote a simple "for" loop to calculate sines in C and was surprised to find that it wasn't much faster than MATLAB.

There is no general yes or no answer that will apply in all cases. It will depend on what is being done in the loops. In particular, calling a sine function on an array in MATLAB vs doing the same thing in a loop in C/C++ I wouldn't expect any speed gain. MATLAB is basically doing the same thing in the background as your C/C++ code.

James Tursa

Subject: MEX files

From: Florin Neacsu

Date: 13 Jul, 2011 21:35:13

Message: 3 of 6

"John Smith" <johnps@gmail.com> wrote in message <ivl1aq$mo4$1@newscl01ah.mathworks.com>...
> For those that have used mex, (for "for" loops possibly), can anyone tell me if there is any noticeable difference in speed between writing loops in C or C++?
>
> I wrote a simple "for" loop to calculate sines in C and was surprised to find that it wasn't much faster than MATLAB.

Hi,

Matlab has now(it's been for a while) jit acceleration, hence it handles for loops much better. You said you created a "simple" loop so it makes sense that you don't see a big difference.

Regards,
Florin

Subject: MEX files

From: dpb

Date: 13 Jul, 2011 21:40:41

Message: 4 of 6

On 7/13/2011 4:05 PM, John Smith wrote:
> For those that have used mex, (for "for" loops possibly), can anyone
> tell me if there is any noticeable difference in speed between writing
> loops in C or C++?
>
> I wrote a simple "for" loop to calculate sines in C and was surprised to
> find that it wasn't much faster than MATLAB.

For straightforward calculations it will be highly likely they will
generate the same code. Since simple loops are passed to the core
routines which are essentially also the same code it's not particularly
surprising there's not any major gain. What makes significant
difference is generally replacing loops in Matlab with vector
operations, not replacing core code.

--

Subject: MEX files

From: James Tursa

Date: 14 Jul, 2011 06:23:14

Message: 5 of 6

"John Smith" <johnps@gmail.com> wrote in message <ivl1aq$mo4$1@newscl01ah.mathworks.com>...
> For those that have used mex, (for "for" loops possibly), can anyone tell me if there is any noticeable difference in speed between writing loops in C or C++?
>
> I wrote a simple "for" loop to calculate sines in C and was surprised to find that it wasn't much faster than MATLAB.

I will follow-up by stating a case where it is often the case that you *can* do better in a mex routine: A MATLAB for loop that works with a large number of array slices. The reason is that MATLAB typically is forced to copy the data for the array slice before using it in an operation. For a large loop that data copying adds up and can dominate the run time, but a mex routine can avoid a data copy and get at array slices directly without copying the data and potentially run quite a bit faster. For an extreme example of this see the mtimesx_test_nd.m function in this submission:

http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

James Tursa

Subject: MEX files

From: John Smith

Date: 14 Jul, 2011 13:37:11

Message: 6 of 6

"James Tursa" wrote in message <ivm20i$bss$1@newscl01ah.mathworks.com>...
> "John Smith" <johnps@gmail.com> wrote in message <ivl1aq$mo4$1@newscl01ah.mathworks.com>...
> > For those that have used mex, (for "for" loops possibly), can anyone tell me if there is any noticeable difference in speed between writing loops in C or C++?
> >
> > I wrote a simple "for" loop to calculate sines in C and was surprised to find that it wasn't much faster than MATLAB.
>
> I will follow-up by stating a case where it is often the case that you *can* do better in a mex routine: A MATLAB for loop that works with a large number of array slices. The reason is that MATLAB typically is forced to copy the data for the array slice before using it in an operation. For a large loop that data copying adds up and can dominate the run time, but a mex routine can avoid a data copy and get at array slices directly without copying the data and potentially run quite a bit faster. For an extreme example of this see the mtimesx_test_nd.m function in this submission:
>
> http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support
>
> James Tursa

Thanks for the replies! Maybe I should look into trying to vectorize...

Tags for this Thread

No tags are associated with this thread.

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.

Contact us