decrease calculation accuracy to speed up running time
Show older comments
hi, I want to write script but I have huge input and I know if I run this script I have to wait long time to get output.
so I want from MATLAB to calculate all data with medium accuracy to speed up my program.
for example MATLAB calculate pi to 32 digits and I want to calculate it for example only to 10 digits to speed up in calculation.
look! I dont want to display a number with low digits by "format" command.
so , what should I do to decrease calculation accuracy ???
Accepted Answer
More Answers (1)
John D'Errico
on 12 Nov 2019
Edited: John D'Errico
on 12 Nov 2019
You essentially cannot do what you want, with one small caveat. That is, you canot arbitrarily tell MATLAB to do all computations using say 10 digits of precision.
MATLAB works using doubles, for the most part. However, you can use reduced precision, such as single, uint8, int8. For some computations, these lower precision numeric classes will be faster. So you can specify that a variable has that specific class. You cannot tell MATLAB that from now on, ALL computaitons will be done using single precision or int8..
What you would need to do is make sure that all variables that you create are SINGLEs, so roughly 8 significant digits in floating point arithmetic. Most computations support singles (although I cannot claim that ALL computations will work there. There is always an exception, so it seems.) Once that is done, then you will find things work somewhat faster. And since you claim this is a large problem, it will take half as much memory, so that in itself may make your code run more quickly.
However, I would point out that not all computations can be done well in lower precision. It is easy to write poor code that will fail in any precision. Good use of numerical methods is important. In fact, if you look at Cleve Moler's blogs, you will see he has talked about very low precision floating point arithmetic. But the difference is Cleve also knows when what he is doing will fail in low precision, and he knows what signs to look for when it would/did fail. Anyway, if you are asking these questions, that means you are not Cleve.
So you CAN use singles, or perhaps int8 arithmetic, if you are using integers.
D = rand(4000);
S = single(D);
timeit(@() qr(D))
ans =
0.36242
timeit(@() qr(S))
ans =
0.16466
You can gain some speed, as you can see. Knowing what you are doing will be highly important though.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!