Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: MEX SLower than MATLAB?
Replies: 0

 Jerome Posts: 48 Registered: 12/9/11
MEX SLower than MATLAB?
Posted: Dec 3, 2012 11:33 AM

I have rewrote my matlab function in C using the mex and it++, but my mex implementation is a lot slower than my MATLAB function. I was wondering if anyone could tell me what I am doing wrong.

MATLAB
for idx = 1:length(eps_r)
% Calculate kz
if (idx == 1) || (eps_r(idx) ~= eps_r(idx-1))
v_p = c/sqrt(eps_r(idx)); % m/s - Propogation Velocity
dz = v_p*dt/2;
z_shift = exp(1i*dz*sqrt((repmat(k,1,size(data,2))).^2-(repmat(kx,size(data,1),1)).^2));
end

fk_data(idx,:) = ifft(mean(data))*exp(-1i*2*pi*freq(1)*time(idx));
data = data.*z_shift;
end

MEX:

for(int idx = 0; idx < eps_r.size(); idx++ )
{
if ( (idx == 1) || (eps_r(idx) != eps_r(idx-1) ) )
{
v_p = 2.9979e+08 / sqrt(eps_r(0));

dz = v_p * time(0)/2;
k = 2 * w / v_p;

for(int y = 0 ; y < z_shift.size(); y++)
z_shift(y) = exp(dz * i * sqrt(pow(z_shift_pt1(y),2) - pow(z_shift_pt2(y),2)));
}

fk_data = ifft(complex_mean(data)) * exp(-i * 2 * itpp::pi * freq(0) * time(idx));
data = elem_mult(data,z_shift);

}