Calling Matlab fun from Python 2 is very slow

618 views Asked by At

I am trying to call a matlab function from python 2.7 script. I am using Matlab engine for Python(Matlab Engine From Mathwork). But it seems very slow. The last line of my python code( check snippet below), takes 5 minutes but same matlab function takes only 0.5 minute when i run directly in matlab.

When i call this matlab function it takes 4.5 minute until it execute first line of matlab code. That i can not understand. I checked online and found that in general, most of the time goes to start matlab engine but in my case starting a matlab engine takes just 9 sec.

import matlab.engine

eng = matlab.engine.start_matlab()
X = eng.load('Original_Image_mat.mat')
noisy=eng.addnoise(X['image'], 1.35, 4.0, 1.0, 3.5, 2.0, 2)

This problem become worse, when the size of the variable X increases from 100 mb to 400 mb. Then it takes 1/2 hr until it execute first line of matlab code, but takes only 2 minutes when i run directly in matlab.

Could anyone explain me why it is slow?

Here is my matlab function( can not post complete function so just posting part of it)

function [ noisy ] = addnoise( vol, angpix, res, falloff, lowpass, padfactor, seed )

rng(seed);  % initialize random numbers
volft = fftshift(fftn(vol));    % get volume FFT
volftabs = abs(volft);
s = size(vol, 1);
[x, y, z] = ndgrid(-s/2:s/2-1, -s/2:s/2-1, -s/2:s/2-1);
r = sqrt(x.^2 + y.^2 + z.^2);   % get distance of each voxel from center
rround = round(r);
0

There are 0 answers