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 thevpa
anddigits
functions 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 matrixC
takes 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 byvpa
to a lower precision of10
digits by usingdigits
. Then, usevpa
to reduce the precision ofC
and findzeta(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 usingdouble
.
For larger matrices, the difference in computation time can be even more significant. For example, consider the1001
-by-301
matrixC
.
[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.