From: Sean on
Hi,

I have an image processing algorithm in Matlab that I converted to C, and the C code ended up being slower than Matlab (note that I have very little experience in writing optimized C code). My question is whether my C code being slower than Matlab code is highly surprising given the following circumstances?

The Matlab code is heavily vectorized and the basic structure is as follows (written in rough pseudocode illustrating some operations that occur inside a main iterative loop).

for i=1:iterations % number of iterations ~200
fftshift(fft2());
upsampling
matrix multiplication,addition,subtraction;
fftshift(ifft2())
end

The operations inside the loop are all vectorized operations on 2D matrices. When converting to C, I code my own matrix operations such as multiplication, addition, upsampling using for loops over each index. For fft's, I use the fftw package.

Is it unsurprising in this case that my C code is slower than Matlab? Would the C code be able to be much faster (>20 times) than Matlab code in this case if I used the BLAS and LAPACK packages instead of coding my own matrix functions?

Thanks! Any advise/opinions would be greatly appreciated.

Regards,
Sean


From: Robert Orzechowski on
Matlab is much faster than C if you use matrices.
C is faster than Matlab if you use many loops. In your example you have one loop, but several martix operations.

regards,
Robert
From: Rune Allnor on
On 23 Nov, 22:55, "Sean " <phido...(a)gmail.com> wrote:

> Is it unsurprising in this case that my C code is slower than Matlab?

Eh... I don't know if I should answer 'yes' or 'no' to that,
so let's just say that it is not at all surprising that your
C code is slower than matlab.

> Would the C code be able to be much faster (>20 times) than Matlab code in this case if I used the BLAS and LAPACK packages instead of coding my own matrix functions?

Matlab uses some of the fastest libraries available for
linear algebra and FFTs. You will need to know exactly
what you are doing to be able to come remotely close to
the performance of these libraries, if you code your own
C code. You will need to know both how to write efficient
C code (one would be required to know parallel programming),
as well as how to trick the compiler into optimizing it
for you. You might even have to hand-optimize some MMX-type
assembler code to get there.

Not for the faint-hearted.

Rune
From: James Tursa on
"Sean " <phidoc09(a)gmail.com> wrote in message <hef0cb$6ve$1(a)fred.mathworks.com>...
>
> for i=1:iterations % number of iterations ~200
> fftshift(fft2());
> upsampling
> matrix multiplication,addition,subtraction;
> fftshift(ifft2())
> end
>
> Is it unsurprising in this case that my C code is slower than Matlab? Would the C code be able to be much faster (>20 times) than Matlab code in this case if I used the BLAS and LAPACK packages instead of coding my own matrix functions?

Not surprising. The matrix addition and subtraction will likely run just as fast in a C loop as in a vectorized MATLAB call (doesn't use BLAS calls for this). For matrix multiplication, *usually* you can't beat the BLAS routines. You can link to them and call them directly from your C code to get the speed improvement directly, or you can use mexCallMATLAB to call the mtimes function to get the speed improvement a bit indirectly. The BLAS routines can easily outperform a hand-written matrix multiply by 20x. I have intentionally coded up a poorly written matrix multiply routine and compared it to the MATLAB BLAS DGEMM routine and it ran 160x slower. My advice is to go through the effort of calling the BLAS routine directly or use mexCallMATLAB to call mtimes.

James Tursa