Solver execution time is very slow.

7 次查看(过去 30 天)
Dmitry
Dmitry 2019-6-18
评论: Dmitry 2019-6-21
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 个评论
Dmitry
Dmitry 2019-6-20
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.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2019-6-20
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 个评论
Dmitry
Dmitry 2019-6-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by