更多关于在Matlab中使用i和j作为变量:速度

时间:2013-08-10 15:37:11

标签: performance matlab complex-numbers

Matlab documentation

  

为了提高速度和改进稳健性,您可以将复杂的i和j替换为1i。例如,而不是使用

a = i;
     

使用

a = 1i;

健壮性部分很明确,as there might be variables called i or j。但是,对于 speed ,我在Matlab 2010b中做了一个简单的测试,我得到的结果似乎与索赔相矛盾:

>>clear all

>> a=0; tic, for n=1:1e8, a=i; end, toc
Elapsed time is 3.056741 seconds.

>> a=0; tic, for n=1:1e8, a=1i; end, toc
Elapsed time is 3.205472 seconds.

有什么想法吗?它可能是与版本相关的问题吗?

在@TryHard和@horchler发表评论之后,我尝试将其他值分配给变量a,结果如下:

  

增加经过时间的顺序:

     

“i”< “1i”< “1 * i”(趋势“A”)

     

“2i”< “2 * 1i”< “2 * i”(趋势“B”)

     

“1 + 1i”< “1 + i”< “1 + 1 * i”(趋势“A”)

     

“2 + 2i”< “2 + 2 * 1i”< “2 + 2 * i”(趋势“B”)

2 个答案:

答案 0 :(得分:10)

我认为你正在看一个病态的例子。尝试更复杂的东西(在OSX上显示R2012b的结果):

(重复添加)

>> clear all
>> a=0; tic, for n=1:1e8, a = a + i; end, toc
Elapsed time is 2.217482 seconds. % <-- slower
>> clear all
>> a=0; tic, for n=1:1e8, a = a + 1i; end, toc
Elapsed time is 1.962985 seconds. % <-- faster

(重复乘法)

>> clear all
>> a=0; tic, for n=1:1e8, a = a * i; end, toc
Elapsed time is 2.239134 seconds. % <-- slower
>> clear all
>> a=0; tic, for n=1:1e8, a = a * 1i; end, toc
Elapsed time is 1.998718 seconds. % <-- faster

答案 1 :(得分:6)

要记住的一点是,无论是从命令行还是保存的M函数运行,都会以不同的方式应用优化。

这是对我自己的测试:

function testComplex()
    tic, test1(); toc
    tic, test2(); toc
    tic, test3(); toc
    tic, test4(); toc
    tic, test5(); toc
    tic, test6(); toc
end

function a = test1
    a = zeros(1e7,1);
    for n=1:1e7
        a(n) = 2 + 2i;
    end
end

function a = test2
    a = zeros(1e7,1);
    for n=1:1e7
        a(n) = 2 + 2j;
    end
end
function a = test3
    a = zeros(1e7,1);
    for n=1:1e7
        a(n) = 2 + 2*i;
    end
end

function a = test4
    a = zeros(1e7,1);
    for n=1:1e7
        a(n) = 2 + 2*j;
    end
end

function a = test5
    a = zeros(1e7,1);
    for n=1:1e7
        a(n) = complex(2,2);
    end
end

function a = test6
    a = zeros(1e7,1);
    for n=1:1e7
        a(n) = 2 + 2*sqrt(-1);
    end
end

运行R2013a的Windows机器上的结果:

>> testComplex
Elapsed time is 0.946414 seconds.    %// 2 + 2i
Elapsed time is 0.947957 seconds.    %// 2 + 2j
Elapsed time is 0.811044 seconds.    %// 2 + 2*i
Elapsed time is 0.685793 seconds.    %// 2 + 2*j
Elapsed time is 0.767683 seconds.    %// complex(2,2)
Elapsed time is 8.193529 seconds.    %// 2 + 2*sqrt(-1)

请注意,结果会随着调用顺序的不同运行而稍微波动。所以,时候吃点盐。

我的结论:如果您使用1i1*i,速度方面无关紧要。


一个有趣的区别是,如果你在函数范围内也有一个变量,你也将它用作虚数单位,那么MATLAB会抛出一个错误:

Error: File: testComplex.m Line: 38 Column: 5
"i" previously appeared to be used as a function or command, conflicting with its
use here as the name of a variable.
A possible cause of this error is that you forgot to initialize the variable, or you
have initialized it implicitly using load or eval.

要查看错误,请将上述test3功能更改为:

function a = test3
    a = zeros(1e7,1);
    for n=1:1e7
        a(n) = 2 + 2*i;
    end
    i = rand();        %// added this line!
end

,即变量i在同一局部范围内同时用作函数和变量。