2

I am working with some python codes that call a Matlab function. The Matlab function takes a 3D array as input and the size is about 87*195*126. I modified the mlarray_sequence.py as suggested in this post. The time spent on converting to Matlab double array does significantly reduce (~0.4 sec). However, it still seems that it takes about 10 seconds to pass the array to the Matlab function, as can be seen by timing each step as follows:

import numpy as np
import matlab.engine
import matlab
from io import StringIO
from os.path import abspath, dirname, join
import timeit

def test1():
    start_time = timeit.default_timer()
    matlabEngine = matlab.engine.connect_matlab()
    directory = abspath(dirname(__file__))
    matlabFolder = directory
    matlabEngine.addpath(matlabFolder)
    matlabEngine.cd(matlabFolder)

    timepoint = timeit.default_timer()
    print('Elapsed time for connecting matlab is {} sec'.format(timepoint - start_time))

    img = np.random.rand(87, 195, 126)
    timepoint1 = timeit.default_timer()
    imgMatlab = matlab.double(img)
    timepoint2 = timeit.default_timer()
    print('Elapsed time for creating matlab array is {} sec'.format(timepoint2 - timepoint1))

    out = StringIO()
    err = StringIO()
    matlabEngine.test2(imgMatlab, stdout=out, stderr=err)
    print(out.getvalue())
    print(err.getvalue())
    timepoint3 = timeit.default_timer()
    print('Elapsed time for matlab script is {} sec'.format(timepoint3 - timepoint2))

test1()
function result = test2(img)

tic
result = 1;
toc

And the output is:

Elapsed time for connecting matlab is 0.0893801 sec
Elapsed time for creating matlab array is 0.40231419999999996 sec
Elapsed time is 0.000603 seconds.
Elapsed time for matlab script is 10.4323424 sec

[Done] exited with code=0 in 11.491 seconds

So is there any way to reduce the time spent on passing the array to matlab function?

P.S.: I have already shared the matlab session by typing matlab.engine.shareEngine before I run the script.

5
  • Do you already have a shared Matlab session running? I guess not, so the most time in the function is probably spend with starting the engine. It is probably a better idea to start the Matlab session outside the function, and keep the python instance open (e.g python -i yourfile.py) Commented Sep 13, 2019 at 9:38
  • Easy check if to add an split_time after initializing the Matlab engine, than you can check what is taking up the most time within the function. Commented Sep 13, 2019 at 9:40
  • @rinkert Yes, I have already shared the maltab session by typing matlab.engine.shareEngine before I run the script. So it should not be the startup time that's taking so long. Commented Sep 13, 2019 at 13:35
  • Can you please verify that by timing? Commented Sep 13, 2019 at 13:40
  • @rinkert Do you mean the time for connecting to matlab? I have updated the code above and you can see that it only takes 0.08 sec to connect to matlab. And the 10.43 sec is spent on the line matlabEngine.test2(imgMatlab, stdout=out, stderr=err), which essentially does nothing in matlab.. Commented Sep 13, 2019 at 14:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.