Open Side Menu Go to the Top
Register
Converting code from MATLAB to C/C++ Converting code from MATLAB to C/C++

03-31-2011 , 04:03 PM
I'm in the middle of a project in which I'm converting some MATLAB code to C/C++ for performance reasons. Does anyone have any advice on how to do this in general? Array indexing is annoying, but not really a problem. I guess I'm just looking for any tips and techniques people may have since a lot of you have much more programming experience than I do and this is my first time doing this sort of task.
Converting code from MATLAB to C/C++ Quote
03-31-2011 , 05:23 PM
Quote:
Originally Posted by Adam27X
I'm in the middle of a project in which I'm converting some MATLAB code to C/C++ for performance reasons. Does anyone have any advice on how to do this in general? Array indexing is annoying, but not really a problem. I guess I'm just looking for any tips and techniques people may have since a lot of you have much more programming experience than I do and this is my first time doing this sort of task.
My only advice is you should really know what you are doing, and know exactly why and by how much the performance will increase if you do this. Because there's a good chance it's not worth the time.
Converting code from MATLAB to C/C++ Quote
03-31-2011 , 06:54 PM
I think MATLAB has the ability to generate java source I would convert to java and then see if that is fast enough if not going from java to c++ should be fairly trivial.
-Phil
Converting code from MATLAB to C/C++ Quote
04-01-2011 , 04:25 AM
Quote:
Originally Posted by gaming_mouse
My only advice is you should really know what you are doing, and know exactly why and by how much the performance will increase if you do this. Because there's a good chance it's not worth the time.
+1. You might be unpleasantly surprised to learn when you convert it that Matlab actually has a pretty solid implementation.
Converting code from MATLAB to C/C++ Quote
04-01-2011 , 05:36 AM
Quote:
Originally Posted by Gullanian
+1. You might be unpleasantly surprised to learn when you convert it that Matlab actually has a pretty solid implementation.
It really depends on what you're doing. But, there are quite a few scenarios in which C++ code is a lot faster, often by an order of magnitude or more. I'm saying this based on my own experience. And I would call myself a proficient MATLAB programmer and a so-so C++ programmer. So I'm not comparing "naive" MATLAB code that misses out on important optimizations (e.g., vectorization, pre-allocation, etc.) vs efficient C++ code. On the other hand (as has been mentioned by another poster), programming in C++ is much more time-consuming.

I would suggest two things to the OP:

1.) Run your MATLAB code through the profiler. Figure out what takes most execution time.
- Is much time spent on routines (functions etc.) you programmed yourself? If so, can you make those routines more efficient? Can you parallelize them? If not, and they are too slow, consider porting to C++.
- If, instead, much time is spend on MATLAB-provided stuff, look at what type of stuff it is. MATLAB built-in functions are often relatively fast. MATLAB functions which make library calls (to BLAS/LAPACK, FFTW, etc.) are particularly fast and gains from porting them tend to be small. It's functions (and scripts) written in the MATLAB language itself where I would hope for the largest gains (by optimizing MATLAB code or by switching to C++).

2.) If you determine that switching to C++ is worthwhile, make sure that your MATLAB code is highly modular. It tremendously speeds up coding and bug-hunting in C++. Lastly, if only one or two of your MATLAB routines are slow, you can consider porting only those (read up on external interfaces/MEX files).
Converting code from MATLAB to C/C++ Quote
04-01-2011 , 01:05 PM
The code is all eventually to be ported to CUDA at a later time and I have a pretty solid grasp on what's going on. Video processing in MATLAB is much slower than by OpenCV in C++. The MATLAB code I'm converting actually is a MEX file, so it already has some C in it (and thus, some of the work has been done for me).

The profiler is something I haven't thought of though, and could be useful in determining what bottlenecks the current implementation.
Converting code from MATLAB to C/C++ Quote
04-01-2011 , 07:43 PM
You want to translate your code to C++, but still do indexing on arrays Why? Pointers figures to be alot faster right?

Havent done anything in MATLAB since I took numerical analysis, but what threefiddy says seems like good advice
Converting code from MATLAB to C/C++ Quote
04-01-2011 , 08:52 PM
hmm, not sure I understand:

Code:
char *A;
A = malloc(NUMBER_OF_ELEMENTS*sizeof(char));
Is using A[i] here slower than dereferencing the pointer?
Converting code from MATLAB to C/C++ Quote
04-01-2011 , 10:42 PM
Quote:
Originally Posted by Adam27X
hmm, not sure I understand:

Code:
char *A;
A = malloc(NUMBER_OF_ELEMENTS*sizeof(char));
Is using A[i] here slower than dereferencing the pointer?
http://stackoverflow.com/questions/2...ys-vs-pointers

Juk
Converting code from MATLAB to C/C++ Quote

      
m