f



Replacing a MATLAB code with a C++ code

I made a program which consists of tens of .m files and I found some
parts of this program are making the whole program slow.

I want to replace those time-wasting parts with C++ codes. I think
C++ codes are a bit faster than MATLAB codes. :)

Is this possible? If possible, can anybody teach me how to do this?

(Or, If there's other way to improve the speed, please let me know.)

Thank you.
Kevin.
0
room317 (3)
7/19/2004 8:09:24 AM
comp.soft-sys.matlab 211266 articles. 24 followers. lunamoonmoon (257) is leader. Post Follow

4 Replies
887 Views

Similar Articles

[PageSpeed] 3

Kevin Kim wrote:
> I made a program which consists of tens of .m files and I found some
> parts of this program are making the whole program slow.
> 
> I want to replace those time-wasting parts with C++ codes. I think
> C++ codes are a bit faster than MATLAB codes. :)
> 
> Is this possible? If possible, can anybody teach me how to do this?
> 
> (Or, If there's other way to improve the speed, please let me know.)

help profview
help mex

-Herbert
0
7/19/2004 8:41:06 AM
Kevin Kim <room317@hanmail.nospam.net> wrote:

> I want to replace those time-wasting parts with C++ codes. I think
> C++ codes are a bit faster than MATLAB codes. :)
 
If you use a lot of loops, then this is certainly true. 
In 99% of the cases, Matlab loops can be replaced 
with vectorized statements. Since Matlab uses highly optimized
numerical libraries (e.g. lapack) and leaves no room for
pointer aliasing, chances are the Matlab code will  
execute faster. Vectorized Matlab code can be a lot
faster than C++.

In order to code Matlab or Fortran95/2003 efficiently,
you should express all parallel computations as
vector statements. Languages like C, Java and C++ 
teaches you to solve inherently parallel computations with
iterative loops. This is a very bad habit, and one that
is very hard to unlearn. 



S.M.
0
sturla1 (30)
7/19/2004 9:33:29 AM
"Kevin Kim" <room317@hanmail.NOSPAM.net> wrote in message
news:eee38bf.-1@webx.raydaftYaTP...
> I made a program which consists of tens of .m files and I found some
> parts of this program are making the whole program slow.
>
> I want to replace those time-wasting parts with C++ codes. I think
> C++ codes are a bit faster than MATLAB codes. :)
>
> Is this possible? If possible, can anybody teach me how to do this?
>
> (Or, If there's other way to improve the speed, please let me know.)
>
> Thank you.
> Kevin.

Sturla is right. You might want to post the offending code here and see if
our collective creativity can come up with a faster solution in MATLAB
first. If not... then it's time for mex-files.


0
7/19/2004 11:00:42 AM
sturla wrote:
>
>
> Kevin Kim <room317@hanmail.nospam.net> wrote:
>
>> I want to replace those time-wasting parts with C++ codes. I
> think
>> C++ codes are a bit faster than MATLAB codes. :)
>
> If you use a lot of loops, then this is certainly true.
> In 99% of the cases, Matlab loops can be replaced
> with vectorized statements. Since Matlab uses highly optimized
> numerical libraries (e.g. lapack) and leaves no room for
> pointer aliasing, chances are the Matlab code will
> execute faster. Vectorized Matlab code can be a lot
> faster than C++.
>
> In order to code Matlab or Fortran95/2003 efficiently,
> you should express all parallel computations as
> vector statements. Languages like C, Java and C++
> teaches you to solve inherently parallel computations with
> iterative loops. This is a very bad habit, and one that
> is very hard to unlearn.
>
>
>
> S.M.
>

You are right. As you expected, the trouble-making parts consist of
lots of loops. I think I have to try to vectorize those
time-consuming parts now. If I fail to vectorize the statements, let
me post the code and ask for some help. :)

Thank you all.
Kevin.
0
room317 (3)
7/20/2004 9:13:52 AM
Reply: