why are matlab symbolic calculations so slow?
Asked Answered
I

0

8

I was testing with the symbolic toolbox of Matlab. And wonder why it is so slow even when just calculating with symbolic numbers. To test it I wrote this

syms x

x = subs(x,1);
a = 1;

tic
for i=1:10000
    z_sym = x + 1;
end
toc

tic
for i=1:10000
    z_num = a + 1;
end
toc

For the first loop it says Elapsed time is 4.358483 seconds. and for the second on Elapsed time is 0.000029 seconds.. I know using the subs function gives me x=1 but it’s still of the typ sym and I could convert it with a cast to speed things up. But that’s the point.

Why it takes around 15000 times longer to calculate 1+1 symbolic than it takes numeric. What is slowing Matlab so hard?

Infralapsarian answered 21/11, 2014 at 10:0 Comment(7)
Because a simple numerical addition is something a processor can do easily, while using symbolics the compiler needs to parse it, check if its a variabel, apply the mathemathical rules that lie on it ,.... Computer are nice for NUMERICAL calculations, but not for ANALYTICAL calculations. Its pretty amazing that they can do Analytical in the first place.Taliesin
Of course numerical addition is faster than symbolic. But 15000 times faster seems a little high for just an addition. I would like to know why the difference is this big.Infralapsarian
I dont think so. very likely Matlab deals with that sentence as he would deal with a 200 character nonlinear Hilbert space equation. There are A LOT of things he need to check in order to understand analytic equations before he decides that just putting 1 and doing 1+1 is good enough. Additionally, numerical calculations are extremely fast in Matlab, sometimes suprisingly even faster than C code (e.g.:#18593841)Taliesin
You say Matlab code is sometimes even faster than C code and post a topic where someone runs C code without any optimization flags in debug mode? I don’t think this should be “surprising” in any way.Infralapsarian
A piece of the answer is that the numeric version was very likely optimized, while the symbolic was not. But the bigger piece of the answer is that your intuition about the computational difficulty of symbolic math is just wrong.Scoutmaster
@Infralapsarian inded you are rigth. I didnt read the topic as good as a shoudl! but thats another thing. Still the symbolic-numerical relationship applies! However, Matlab can get as fast as C in some cases as Matlab is writen in C (some things)Taliesin
What @Scoutmaster said. The numeric for loop is almost certainly JIT compiled to faster code (forget what you've been told: for loops can be very fast in Matlab). The symbolic for loop is almost certainly not. This is a worst case scenario, but also not a very good example of how one would use symbolic math in practice. You can vectorize symbolic expressions or use specialized native functions in many case to get much better performance. In most cases, you'll still see a 2–3 order of magnitude difference in speed though. Symbolic math uses a lot more memory too.Apperception

© 2022 - 2024 — McMap. All rights reserved.