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).