Solver execution time is very slow.

8 visualizzazioni (ultimi 30 giorni)
Dmitry
Dmitry il 18 Giu 2019
Commentato: Dmitry il 21 Giu 2019
This code:
tic
syms a1 a2 a3
eqns = [a1 + 0.01 * (a2 + a3) - 5, a2 + 0.01 * (a1 + a3) - 6, a3 + 0.01 * (a2 + a1) - 7];
sol = solve(eqns, [a1 a2 a3]);
toc
shows execution time
Elapsed time is 0.261647 seconds.
or near this value.
In a loop this time cuts twice up to 0.12 seconds.
BUT!
The same solver in Python in Google Colab
%%timeit
a = np.array([[1, 0.01, 0.01], [0.01, 1, 0.01], [0.01, 0.01, 1]])
b = np.array([5, 6, 7])
c = np.linalg.solve(a, b)
shows time
The slowest run took 60.49 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 12.8 µs per loop
which is 10 000 times FASTER.
WHY?
Why MATLAB is so slow and how to make it faster??
  1 Commento
Dmitry
Dmitry il 20 Giu 2019
Replaced solve by '\'
This code:
%%
a = [1 0.01 0.01; 0.01 1 0.01; 0.01 0.01 1];
b = [5 6 7]';
%%
tic
x = a\b;
toc
works 1000 times faster
Elapsed time is 0.000161 seconds.
Elapsed time is 0.000107 seconds.
Elapsed time is 0.000189 seconds.
Elapsed time is 0.000105 seconds.
Elapsed time is 0.000105 seconds.
Elapsed time is 0.000117 seconds.
Elapsed time is 0.000126 seconds.
Elapsed time is 0.000111 seconds.
but still 10 times slower than python in Colab.
And nearly 30 times slower than numpy + numba python libs.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 20 Giu 2019
tic
syms a1 a2 a3
eqns = [a1 + 0.01 * (a2 + a3) - 5, a2 + 0.01 * (a1 + a3) - 6, a3 + 0.01 * (a2 + a1) - 7];
sol = solve(eqns, [a1 a2 a3]);
toc
The first time that a symbolic reference is created after you clear the session, the symbolic engine needs to start up. You are including that start-up time.
You are defiing symbolic equations and asking the symbolic solver to find indefinitely-precise solutions. The symbolic solver has to evaluate for exact solutions using software mathematics in order to retain the indefinite precision. Your np.linalg.solve call, on the other hand, is processing pure numeric double-precision arrays.
  1 Commento
Dmitry
Dmitry il 21 Giu 2019
solve function - is the main google result on "matlab solve system of equations" request.
Most of them who want to solve simple linear system will use it. And nobody cares of execution time of single calculation.
But I needed this in a loop and faced that speed limits.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by