Python Matlab engine randomly freeze

5 次查看(过去 30 天)
Hi,
I'm planning to interact with MATLAB from Python (Pytorch) to do some machine learning. To get my hands on I'm testing it on a toy case (Lorenz attractor) but I'm facing a weird behavior.
I have one script in Python and one in MATLAB, both doing exactly the same thing : calling RK4/Lorenz.
In MATLAB everything runs smoothly (obviously) :
Elapsed time is 0.000041 seconds.
Elapsed time is 0.000043 seconds.
Elapsed time is 0.000060 seconds.
Elapsed time is 0.000071 seconds.
Elapsed time is 0.000045 seconds.
Now in Python, if I look at the execution time from MATLAB only (inside the function) it's ok :
Elapsed time is 0.000054 seconds.
Elapsed time is 0.000096 seconds.
Elapsed time is 0.000049 seconds.
Elapsed time is 0.000060 seconds.
Elapsed time is 0.000054 seconds.
The problem is if I look at the execution time of the MATLAB function from the Python script this time :
Python call 0.013752460479736328
Python call 0.18750524520874023
Python call 0.005811214447021484
Python call 0.04061627388000488
Python call 0.006798982620239258
Python call 0.8868112564086914
Python call 0.0055887699127197266
Python call 0.16828298568725586
Python call 0.003731250762939453
Looks like it's getting randomly slowed down/stucked. Here is the Python code :
import matlab.engine
from time import time
eng = matlab.engine.start_matlab()
timestep = 0.01
duration = 0.2
n_step = int(duration // timestep)
data = eng.zeros(n_step, 3)
data[0] = [10, 2, 3]
for i in range(n_step - 1):
start = time()
data[i + 1] = eng.RK4(data[i], 10.0, 8/3, 28.0, timestep)[0]
t = time() - start
print("Python call", t)
eng.quit()
Am I doing something wrong or is it a known issue of the engine?
Thank you,
Thibaut

回答(1 个)

Riya
Riya 2024-1-25
Hi
When you invoke `matlab.engine.start_matlab()`, you're initiating a new MATLAB session. This operation is inherently time-consuming, as it involves starting up the entire MATLAB environment.
The perceived inefficiency isn't due to Python; rather, it's because you're comparing the initialization time of a MATLAB session with the execution time of a function call within that session.
To get a more accurate measure of the function's execution time, you should move the `start =` line to immediately before the `eng.forward` call. This will capture the time taken by the function itself, excluding the MATLAB startup time.
For more information refer the following article:

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by