MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   故意降低了MATLAB函数的速度? (https://www.labfans.com/bbs/showthread.php?t=22839)

poster 2019-12-10 16:49

故意降低了MATLAB函数的速度?
 
我想为[URL="http://en.wikipedia.org/wiki/MATLAB"]MATLAB[/URL]编写一个非常慢的程序。我说的是O(2 ^ n)或更糟。它必须完成,并且必须确定性地缓慢,因此不要“如果rand()= 123,123,请退出!”这听起来很疯狂,但实际上是针对分布式系统测试的。我需要创建一个.m文件,使用[URL="http://en.wikipedia.org/wiki/MCC#Technology"]MCC对其进行[/URL]编译,然后在我的分布式系统上运行它以执行一些调试操作。

该程序必须一直在工作,因此sleep()不是有效的选项。

我尝试制作一个随机的大矩阵并找到其逆矩阵,但这完成得太快了。有任何想法吗?


回答:
在我的1.86 GHz单核计算机上,对于2048长的输入向量x,这种离散傅立叶变换的简单实现耗时约9秒。输入4096个输入可将时间延长至〜35秒,接近于O(N ^ 2)的4倍。我没有耐心尝试更长的输入内容:)

function y = SlowDFT(x) t = cputime; y = zeros(size(x)); for c1=1:length(x) for c2=1:length(x) y(c1) = y(c1) + x(c2)*(cos((c1-1)*(c2-1)*2*pi/length(x)) - ... 1j*sin((c1-1)*(c2-1)*2*pi/length(x))); end end disp(cputime-t); [B]编辑:[/B]或如果您要强调内存比CPU:

function y = SlowDFT_MemLookup(x) t = cputime; y = zeros(size(x)); cosbuf = cos((0:1:(length(x)-1))*2*pi/length(x)); for c1=1:length(x) cosctr = 1; sinctr = round(3*length(x)/4)+1; for c2=1:length(x) y(c1) = y(c1) + x(c2)*(cosbuf(cosctr) ... -1j*cosbuf(sinctr)); cosctr = cosctr + (c1-1); if cosctr > length(x), cosctr = cosctr - length(x); end sinctr = sinctr + (c1-1); if sinctr > length(x), sinctr = sinctr - length(x); end end end disp(cputime-t); 这比在每次迭代中计算sin和cos更快。 2048长的输入大约需要3秒,而16384长的输入大约需要180秒。



[url=https://stackoverflow.com/questions/1616130]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:27

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.