views:

1168

answers:

7

Hey there

I have some Matlab image processing code which runs pretty slowly and I'm prepared to convert it over to C/C++. I don't really know much about how matlab works and how code is executed but I'm just interested to hear what kind of speedups I might expect. Clearly there are many variables that will affect this but I'm just looking for a guide perhaps from your own experiences.

Thanks

Zenna

+4  A: 

It really depends on the quality of your Matlab code and what it is you are doing. Idiomatic Matlab code written by a Matlab expert is going to be hard to beat, particularly if you're not an optimization guru and purely expecting a speed up due to the switch of language. For instance, I found even some of the more well regarded C-based FFT libraries were no match for Matlab's FFT.

That said, comparing a poorly written Matlab program to an averagely written c++ one, I'd say you're looking at an order of magnitude in my experience.

Andy
For FFTs, Matlab uses FFTW ("the fastest Fourier transform in the West," see http://www.fftw.org ), which is implemented in C (actually, C code generated by Objective Caml, see http://www.fftw.org/pldi99.pdf ).
las3rjock
+3  A: 

The short answer to the question of what kind of speedups you may get is "it depends".

Matlab is an interpreter, so overall it is much slower than native c++ code. However, many matlab functions are well optimized, and recent versions include JIT. So you would have to decide whether to rewrite all of your matlab code in C, rewrite only the critical parts, or to optimize the matlab code itself to run faster.

I would suggest that you start by using Matlab's built-in profiling tools to find the performance bottlenecks in your application. It may be the case that you may tweak the matlab code to get better performance. The rule of thumb is to avoid loops by using vectorized array operations instead of iterating one element at a time.

Dima
With the introduction of JIT accelerator, the "for loop penalty" is not really of concern like in the past. Use the profiler to find the true bottlenecks.
MatlabDoug
+1  A: 

For instance matlab uses the FFTW library to implement fft algorithms. The performance of that library is almost impossible to beat. The only one I know that is comparable is the intel Math Kernel Library (MKL), but is commercial. So first of all I would suggest to use every of mathematical libray you can find. Matlab is doing that behind the scenes.

It is true that it is sometimes difficult to beat matlab. But the thing is that the matlab profiler not always gives you enough information about how to improve your code. You know that some matlab methods are taking most of the time but you don't always know if the are ways to improve the performance calling them in another way because that method is a black-box.

In C/C++ you have tools like valgirnd that allows you to inspect even if the assembler that the compiler is generating and that way you can help the compiler to improve that code inlining a method for instance. But again matlab is using professional mathematical libraries behind the scenes and if most of time is spend on those libraries when you execute your matlab code the performance then is difficult to improve.

I would you could try a different approach. You can analyse the bottlenecks with matlab profiler an see if it would be worth it to move that code to native code. Matlab allows you that. You can also do it the other way around. You can implement some glue in C/C++ and and call matlab for some operations where you have experienced that your native code is slower that matlab.

fco.javier.sanz
+1  A: 

For image processing you can get a noticable speedup. But this really depends on how good you are at writing MATLAB code. Lots of things can be vectorized or be taken care of by built in functions. That kind of code is blazing fast.

However, if you find your code to consist of a lot of loops (like say, looping over all pixels in an image) it is going to be incredibly slow and vectorization can give 100x speedup or more.

If your code is very hard to do "right" in MATLAB, then switching to C can be a viable option. I did a computer vision project at school (3D point reconstruction) which clearly showed this. When our project, which was implemented in C++ and OpenCV, was finished calculating, one of the other groups projects had hardly even loaded the images yet. Theirs were written in MATLAB. We never timed it, but my guess is that our version ran about 10 times faster.

But then again, their MATLAB code was probably not optimized at all. So it's not really useful as a benchmark.

kigurai
With the introduction of JIT accelerator, the "for loop penalty" is not really of concern like in the past. Use the profiler to find the true bottlenecks.
MatlabDoug
Yes, I've heard that as well. Unfortunately I have not had the pleasure of working with the newest MATLAB versions yet :(
kigurai
+5  A: 

It mostly depends on the tightness of your loops in Matlab. If you are simply calling a series of built-in Matlab image processing functions, you will most likely not be able to improve performance (most likely you will hurt it). If you are looping over image pixels or doing some kind of block processing, you may see big improvements. If you are doing some looping, but the amount of processing within each iteration is substantial, you may only see little or no improvement.

The way I look at Matlab is that every executed line has some amount of overhead. If you can put your solution into the form of a matrix multiply, or some other vector/matrix operation, you only suffer that overhead once and it is negligible. However, with loops, you suffer that overhead every time the loop iterates. Also, most of Matlab's image processing functions are just making calls out to optimized libraries, so don't try to recreate them unless you know for sure where they can be improved.

I found that the best approach is to use a combination of C and Matlab. I use Matlab when the operation can be easily vectorized (put in terms of vector/matrix operations). This may mean coming at the solution from a different angle than what seems the most straightforward. Also, it is hard to beat Matlab's plotting and visualization so I would definitely not move to an all C/C++ solution unless you have a plan for how to display with C/C++ (if that is part of your project).

If I can't come up with a relatively easy way to vectorize, I just implement the part of processing that needs tight loops in a C mex function that can be called from Matlab. I tend to use C instead of C++ in this case since the process should be relatively small and not need a lot of complicated data abstraction, but C++ would work fine too. Make sure you access image data in column-major order to maximize cache hits since this is how Matlab organizes its matrices.

Jason
With the introduction of JIT accelerator, the "for loop penalty" is not really of concern like in the past. Use the profiler to find the true bottlenecks.
MatlabDoug
Yeah, I'd definitely go that route first, but I have still found situations where C with optimizations can be better. It could just be my limited knowledge on taking advantage of the JIT compiler though. That being said, avoiding C/C++ where Matlab can do as well or better is a good idea.
Jason
A: 

Like others have said, use the MATLAB profiler to see what the bottlenecks are. If it's matrix number-crunching, you've got a pretty high bar to jump over to beat MATLAB. If you've got a lot of conditional statements or function calls, you are more likely to be able to improve your speed.

Make sure you try to minimize the number of times data is transferred between MATLAB and C++. If you are sending large data arrays in one big clump, it's likely to be quick. Otherwise if you do lots of data transfers back and forth, even if your C++ program is fast, you may lose the speed advantage in the data conversion.

I would also look at your algorithms and consider using Java. It is very convenient to call custom Java code from MATLAB, since MATLAB already runs on a JRE. I've been very impressed by the speed of transferring large data arrays between MATLAB functions and my custom Java code. I had looked at implementing an algorithm in straight C++ (using MEX or whatever) a few years ago to speed up MATLAB, and it just looked like a nightmare to deal with all the data structures. I ended up using COM/ActiveX instead, because I was running on a Windows machine and the interfacing was a lot easier.

After having done a lot of low-level programming to solve numerical problems, I have a better appreciation for what can go wrong, from numerical accuracy to programming maintenance issues, and unless there were a huge performance advantage, I'd choose a higher level language any day over C/C++.

Jason S
+1  A: 

Hi, I have export a matlab routine in c++ and compile with visual studio c++ as mex. The speedup was a factor of 10. and if i would use multi cores, then i would have propably also 3 times more speed.

If you have slopes in slopes and do something with the single components of the matrix, something like y(m,n) = x(m) * a - x(m-1) and this in for slopes then you will have a good speed up.

if you use many matlab function for calculation, where the matlab function itself do many operations, then it makes less sense to export code in c++.

Jochen G.