Main Content

Increase Speed by Reducing Precision

Increase MATLAB®’s speed by reducing the precision of your calculations. Reduce precision by using variable-precision arithmetic provided by the vpa and digits functions in Symbolic Math Toolbox™. When you reduce precision, you are gaining performance by reducing accuracy. For details, see Choose Numeric or Symbolic Arithmetic.

For example, finding the Riemann zeta function of the large matrix C takes a long time. First, initialize C.

[X,Y] = meshgrid((0:0.0025:.75),(5:-0.05:0));
C = X + Y*i;

Then, find the time taken to calculate zeta(C).

tic
zeta(C); 
toc
Elapsed time is 340.204407 seconds.

Now, repeat this operation with a lower precision by using vpa. First, change the precision used by vpa to a lower precision of 10 digits by using digits. Then, use vpa to reduce the precision of C and find zeta(C) again. The operation is significantly faster.

digits(10)
vpaC = vpa(C);
tic
zeta(vpaC);
toc
Elapsed time is 113.792543 seconds.

Note

vpa output is symbolic. To use symbolic output with a MATLAB function that does not accept symbolic values, convert symbolic values to double precision by using double.

For larger matrices, the difference in computation time can be even more significant. For example, consider the 1001-by-301 matrix C.

[X,Y] = meshgrid((0:0.0025:.75),(5:-0.005:0));
C = X + Y*i;

Running zeta(vpa(C)) with 10-digit precision takes 15 minutes, while running zeta(C) takes three times as long.

digits(10)
vpaC = vpa(C);
tic
zeta(vpaC);
toc
Elapsed time is 886.035806 seconds.
tic
zeta(C);
toc
Elapsed time is 2441.991572 seconds.

Note

If you want to increase precision, see Increase Precision of Numeric Calculations.