我使用的是具有64GB RAM的Xeon四核处理器。运行该功能的程序只有89个数据点。现在已经超过20分钟了,MATLAB仍然忙着#34;计算程序。下面的代码是否显示出为什么要花这么长时间来计算?
function last15MinsOfDay=last15MinsOfDay(time,price)
% last15MinsOfDay takes the average of prices between 3:45 and 4:00.
timeStr=cellstr(datestr(time));
timeDbl=datevec(timeStr);
times=and(timeDbl(:,4)==14,timeDbl(:,5)>=45)+and(timeDbl(:,4)==15,timeDbl(:,5)==0);
priceIdx=find(times);
z=find(fwdshift(1,priceIdx)~=priceIdx+1);
z=[1; z];
mu=zeros(length(z),1);
for i = 1:length(z);
while i < length(z)
mu(i)=mean(price(priceIdx(z(i):z(i+1))));
end
end
last15MinsOfDay=mu;
答案 0 :(得分:4)
我不是matlab的专家,但这部分看起来很有趣:
<ion-view view-title="Newest posts">
<div class="bar bar-subheader
item-input-inset bar-light">
<label class="item-input-wrapper">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" ng-model="query" placeholder="Search for post">
</label>
</div>
<ion-content class="has-subheader">
<ion-refresher on-refresh="load()">
</ion-refresher>
<ion-list>
<ion-item ng-repeat='post in posts track by post.id | filter: query ' class='item-thumbnail-left item-text-wrap' href="#/tab/list/{{post.id}}">
<img ng-src='{{post.photo}}' />
<div>
<p class='shortText titleArticle'>
{{post.title | limitTo: 33}}
{{ post.title.length > 33 ? '…' : '' }}
</p>
<img class='articleAuthImg' src="img/StoyanGenchev.jpg" alt="StoyanGenchev-author" />
</div>
<div class='articleInfo'>
<h2>{{post.author}}</h2>
<h4>{{post.date}}</h4>
<h4 class="category">{{post.category}}</h4>
</div>
<div class="clear"></div>
<p class='shortText'>
{{post.description | limitTo: 200}}
{{ post.description.length > 200 ? '…' : '' }}
</p>
</ion-item>
</ion-list>
<ion-infinite-scroll
ng-if="noMore()"
on-infinite="get_more()"
distance="15%"
>
</ion-infinite-scroll>
</ion-content>
</ion-view>
具体来说,我没有看到for i = 1:length(z);
while i < length(z)
mu(i)=mean(price(priceIdx(z(i):z(i+1))));
end
end
在内循环中递增,因此内循环将无限期地运行。
答案 1 :(得分:3)
为了补充esm的答案,你有两个循环,每个循环使用相同的变量i
,所以这会导致你的while循环出错,因为变量i
被重新定义每次迭代的for循环(在你应用esm的校正之后)。要查看我的意思,请尝试按照代码查看输出:
z = 1:10;
for i = 1:(length(z));
while i < length(z)
disp(['while loop:' num2str(i)]);
i = i+1;
end
disp(['for loop:' num2str(i)]);
end
此外,由于双循环,您通过将相同数据重写为变量m(i)
来浪费大量处理时间
我觉得这是你打算做的事情:
for i = 1:length(z)-1;
mu(i)=mean(price(priceIdx(z(i):z(i+1))));
end
我刚刚删除了嵌套的while循环,因为它并没有真正起作用并耗尽处理时间,然后我将length(z)
更改为length(z) - 1
以避免index out of bounds
错误。
最后,您的脚本将仅使用您的一个处理器内核,因此在此处使用qaud核心处理器并不能为您提供任何速度优势。要使用所有4个核心,请尝试使用parfor
而不是for
,理论上您可以将处理速度提高四倍。
http://nl.mathworks.com/help/distcomp/parfor.html