我在matlab中的代码中遇到了一个小错误,我从单个值(而不是数组)开始集成循环.
我注意到,如果我给trapz(1,1)它几乎立即给出零(就像它应该对于trapz(x,y)),但是它计算它所花费的时间在很大程度上取决于'y'的大小.
那是:
tic;trapz(5.1000,1.6610e+03);toc
Elapsed time is 0.011022 seconds.
tic;trapz(5.1000,1.6610e+04);toc
Elapsed time is 0.485286 seconds.
tic;trapz(5.1000,1.6610e+05);toc
Elapsed time is 46.400199 seconds.
tic;trapz(5.1000,1.6610e+06);toc
..Still going on
Run Code Online (Sandbox Code Playgroud)
我没有解释.为什么输入值是多少?
使用两个标量输入,trapz
将您的呼叫解释为
trapz(y,dim)
跨维度集成dim
的y
要沿着该维度进行集成,它会应用排列permute
.随着dim
增加,这种排列成本更高.这些是相关的trapz
代码行:
perm = [dim:max(ndims(y),dim) 1:dim-1];
y = permute(y,perm);
尝试自己计时:
>> y = 5; dim = 1e3;
tic, perm = [dim:max(ndims(y),dim) 1:dim-1]; y = permute(y,perm); toc
Elapsed time is 0.001761 seconds.
>> y = 5; dim = 1e4;
tic, perm = [dim:max(ndims(y),dim) 1:dim-1]; y = permute(y,perm); toc
Elapsed time is 0.148300 seconds.
>> y = 5; dim = 1e5;
tic, perm = [dim:max(ndims(y),dim) 1:dim-1]; y = permute(y,perm); toc
Elapsed time is 17.534308 seconds.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
120 次 |
最近记录: |