gpt4 book ai didi

performance - 在 MATLAB 中使用矢量-标量乘法进行真正的慢循环

转载 作者:太空宇宙 更新时间:2023-11-03 20:21:11 24 4
gpt4 key购买 nike

Have I done something wrong or is vector-by-scalar multiplication really so costly? Doesn't MATLAB (ver 2012a or higher) optimize the code somehow to prevent such curiosities?

>> tic; for i=1:100000; x = sin(i)*[1; 1]; end; toc;
Elapsed time is 1.338225 seconds.
>> tic; for i=1:100000; x = sin(i).*[1; 1]; end; toc;
Elapsed time is 1.228331 seconds.
>> tic; for i=1:100000; x = [sin(i); sin(i)]; end; toc;
Elapsed time is 0.073888 seconds.
>> tic; for i=1:100000; tmp=sin(i); x = [tmp; tmp]; end; toc;
Elapsed time is 0.072120 seconds.

你能给我什么指导方针来让 MATLAB 中的 FLOPS 花费他们真正需要的时间。

附言。这只是一个示例代码,我所做的是解决 odes 系统,我想在计算所需的差异时优化运行时间。以上让我担心我可能会以非最佳方式做某事。

最佳答案

"Doesn't MATLAB (ver 2012a or higher) optimize the code somehow to prevent such curiosities?"

是的,如果代码在 function m 文件中,因为 JIT 编译器(即时编译器)和/或加速器

然而,如评论和其他答案中所述,如果可能,矢量化通常仍然是更好的选择

直接在命令行上:

tic; for i=1:100000; x1 = sin(i)*[1; 1]; end; toc;
tic; for i=1:100000; x2 = sin(i).*[1; 1]; end; toc;
tic; for i=1:100000; x3 = [sin(i); sin(i)]; end; toc;
tic; for i=1:100000; tmp=sin(i); x4 = [tmp; tmp]; end; toc;
Elapsed time is 1.795528 seconds.
Elapsed time is 1.606081 seconds.
Elapsed time is 0.072672 seconds.
Elapsed time is 0.065904 seconds.

在函数内;

[x1,x2,x3,x4]=foo();
Elapsed time is 0.029698 seconds.
Elapsed time is 0.035248 seconds.
Elapsed time is 0.064080 seconds.
Elapsed time is 0.054499 seconds.

函数 foo 保存为:

function [x1,x2,x3,x4]=foo()

tic; for i=1:100000; x1 = sin(i)*[1; 1]; end; toc;
tic; for i=1:100000; x2 = sin(i).*[1; 1]; end; toc;
tic; for i=1:100000; x3 = [sin(i); sin(i)]; end; toc;
tic; for i=1:100000; tmp=sin(i); x4 = [tmp; tmp]; end; toc;

end

编辑

在试图找到支持上述声明的文档时,我意识到我犯了一个错误,它也加速了脚本 m 文件,因此上面的功能被编辑了

在脚本中;

fooscript;
Elapsed time is 0.033536 seconds.
Elapsed time is 0.033720 seconds.
Elapsed time is 0.066050 seconds.
Elapsed time is 0.058428 seconds.

脚本 fooscript 包含:

tic; for i=1:100000; x1 = sin(i)*[1; 1]; end; toc;
tic; for i=1:100000; x2 = sin(i).*[1; 1]; end; toc;
tic; for i=1:100000; x3 = [sin(i); sin(i)]; end; toc;
tic; for i=1:100000; tmp=sin(i); x4 = [tmp; tmp]; end; toc;

遗憾的是,没有大量关于 JIT 和加速器(如果有的话)的文档。但是,为了进行比较,您可以使用 feature('accel','on'/'off')feature('jit','on'/'off')< 禁用 JIT 或加速。 (注意:禁用 accel 也会禁用 jit,因为它似乎是 accel 的一部分。)

如果禁用 accel,性能改进会减少,但是函数和脚本性能仍然相似,并且仍然明显快于命令行。

禁用 JIT 对性能没有明显影响,因此原来的说法是错误的。

关于performance - 在 MATLAB 中使用矢量-标量乘法进行真正的慢循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24673588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com