decrease calculation accuracy to speed up running time
2 次查看(过去 30 天)
显示 更早的评论
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 ???
4 个评论
采纳的回答
Daniel M
2019-11-12
编辑:Daniel M
2019-11-12
The reason your algorithm is taking so much time is because of the line
COPT(u+1,:)=[];
What happens when you delete an entry to an array is MATLAB has to create an entirely new array in memory. Since you are deleting tens of thousands of entries, you are creating tens of thousands of arrays. The improved way to perform this loop is to simply mark an entry for deletion within the loop, and then delete all the invalid rows in one fell swoop outside the loop.
There may even be a way to vectorize this operation, but I think the following loop method is sufficient and, importantly, instructive. Replace the while loop at the bottom with the following
u = 1;
tbd = false(length(COPT),1); % "to be deleted"
while u <= size(COPT,1)-1
s = 1; % this is the "next" spot
while u+s <= size(COPT,1) && COPT(u,1) == COPT(u+s,1)
COPT(u,2) = COPT(u,2) + COPT(u+s,2);
tbd(u + s) = true; % mark this spot for deletion later
% increment s, and check again until it no longer equals
s = s + 1;
end
u = u + s; % increment u
end
% delete invalid entries in COPT
COPT(tbd,:) = [];
Now even with the large scale input, this takes 0.6 seconds to run on my machine, whereas before it was 10 minutes before I stopped execution (and it was only on iteration 476 out of 451817).
I also ran it on a small scale and ensured that the two algorithms produced the same result.
更多回答(1 个)
John D'Errico
2019-11-12
编辑:John D'Errico
2019-11-12
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!