Differing computing time for simple algebraic operations
1 view (last 30 days)
Show older comments
Dear All, I have the following code:
clc
% parameters and exogenous grids
t = 1;
T = 2;
beta = 0.96;
hmax = 8;
hmin = 1;
nhvfi = 300;
nh = 80;
np = 45;
nS = 3;
nA = 2;
A = [0.25 0.8];
hpvfi = linspace(hmin^(1/2),hmax^(1/2),nhvfi).^2;
h = linspace(hmin^(1/2),hmax^(1/2),nh).^2;
[HPvfi,APvfi] = ndgrid(hpvfi,A);
[Hvfi,Avfi] = ndgrid(h,A);
% matrices that differ between the real code and this code
Pgrid1vfi = ones(np,nh,np,nA,nS);
Pgrid2vfi = ones(np,nh,np,nA,nS);
Pgrid3vfi = ones(np,nh,np,nA,nS);
Vfinal = randn(T,np,nh,np,nA,nS);
Vfinal(Vfinal<0) = NaN;
utilVFI = randn(np,nh,np,nA,nS,nhvfi);
% perplexing operations
tic
pVFI1 = repmat(Pgrid1vfi,1,1,1,1,1,nhvfi);
toc
tic
pVFI2 = repmat(Pgrid2vfi,1,1,1,1,1,nhvfi);
toc
tic
pVFI3 = repmat(Pgrid3vfi,1,1,1,1,1,nhvfi);
toc
% inerpolations
tic
V1 = interpn(Hvfi,Avfi,squeeze(Vfinal(t+1,1,:,1,:,1)), HPvfi, APvfi );
toc
tic
V2 = interpn(Hvfi,Avfi,squeeze(Vfinal(t+1,1,:,1,:,2)), HPvfi, APvfi );
toc
tic
V3 = interpn(Hvfi,Avfi,squeeze(Vfinal(t+1,1,:,1,:,3)), HPvfi, APvfi );
toc
% perplexing operations
tic
a1 = permute(repmat( V1 ,1,1,np,nh,np,nS ), [3 4 5 2 6 1] );
toc
tic
a2 = permute(repmat( V2 ,1,1,np,nh,np,nS ), [3 4 5 2 6 1] );
toc
tic
a3 = permute(repmat( V3 ,1,1,np,nh,np,nS ), [3 4 5 2 6 1] );
toc
% perplexing operation
tic
XYZ = utilVFI + beta * ( pVFI1 .* a1 + pVFI2 .* a2 + pVFI3 .* a3 ) ;
toc
In this code there are operations that consume some time but every time I run my code they take, more or less, the same amount of time. In my real code, in which matrices utilVFI, Vfinal and Pgrid(1/2/3)vfi differ from the ones presented here, happens something strenge: the computing time differs SIGNIFICANTLY almost each time I run the code. The last operation can take either 5s or 143s, all other operations (but interpolations) also can differ significantly in their duration (eg. 3.5s vs 67s). The most important difference between matrices is that Vfinal and utilVFI have full rows of NaN while here Vfinal has just random NaN here and there.
Can anyone explain this? The strange thing is that time differences are 'random-like', the same operation can take either 4s or 90s. Why can the last operation, which is just a summation and multiplication of matrices, take so long ? Why can very similar operations take extremely different computing time ?
0 Comments
Answers (1)
John D'Errico
on 9 Mar 2017
Without even looking at your code, very likely you are running into memory limitations on your computer. If some of the time, MATLAB is forced to do some significant memory swapping, then it will take a LONG time, as disk access is hugely slower than RAM access.
And, since I see some seriously high dimensional arrays, like this:
Vfinal = randn(T,np,nh,np,nA,nS);
that makes my guess possibly accurate.
If I then run your code, and see what size arrays we are talking about:
size(Vfinal)
ans =
2 45 80 45 2 3
>> whos Vfinal
Name Size Bytes Class Attributes
Vfinal 6-D 15552000 double
So Vfinal is 15 megabytes. Not huge. But still possibly an issue.
How much memory do you have available to MATLAB? Do you have a solid state drive, or a mechanical platter?
I would run your code, using a monitor on the system. When things come to a complete stop, is your hard drive thrashing, while the CPU is doing essentially nothing? If so, the problem is clear.
Are you checking your mail on the side, using the web browser? Again, that would be an issue.
Are you using a MATLAB version that is non-local? So it needs network access to run code? That can seriously slow things down, if your local network is occasionally overwhelmed by someone else.
Really, yours is a question that is difficult to answer, as there can be many reasons for your problem. You need to do the detective work here.
0 Comments
See Also
Categories
Find more on Mathematics and Optimization 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!