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 thevpaanddigitsfunctions in Symbolic Math Toolbox™. When you reduce precision, you are gaining performance by reducing accuracy. For details, seeChoose Numeric or Symbolic Arithmetic.

For example, finding the Riemann zeta function of the large matrixCtakes a long time. First, initializeC.

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

Then, find the time taken to calculatezeta(C).

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

Now, repeat this operation with a lower precision by usingvpa. First, change the precision used byvpato a lower precision of10digits by usingdigits. Then, usevpato reduce the precision ofCand findzeta(C)again. The operation is significantly faster.

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

Note

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

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

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

Runningzeta(vpa(C))with 10-digit precision takes 15 minutes, while runningzeta(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 toincreaseprecision, seeIncrease Precision of Numeric Calculations.