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?
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 symbolicfor
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