5

I'm calling MATLAB Functions from Python via the MATLAB Engine for Python, but it is rather slow. The original MATLAB script runs in 30 ms on my system, whereas the python script in little over 5 seconds.

Is there a reason why? What should I do?

The python code:

import matlab.engine,time
start = time.clock()

eng = matlab.engine.start_matlab()
eng.forward


elapsed = (time.clock() - start)
print("Time used:",elapsed)

>>>  'Time used:', 4.879795798557371

The MATLAB runtime:

forward |  0.029 s  |  0.015 s
Adriaan
  • 17,741
  • 7
  • 42
  • 75
LzIManD
  • 53
  • 7
  • Either run matlab code in matlab or switch to [numpy](numpy.org). This would be better asked on superuser or a different coding forum however. – Matthew May 02 '15 at 13:05

1 Answers1

4

The call to matlab.engine.start_matlab() starts a MATLAB session, which will obviously take quite some time.

So it is not so much a question of Python being inefficient, but measuring two different things.

Try moving the start = line to just before the eng.forward call and see what results you get.

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
  • 1
    Yes,most time spent in `eng = matlab.engine.start_matlab()`,it spent `('Time used:', 5.107684033779041)` meantime the `eng.forward ` spent `('Time used:', 1.5189894204439556e-05)` – LzIManD May 02 '15 at 13:34