More on using i and j as variables in Matlab: speed

这一生的挚爱 提交于 2019-12-05 00:48:39

I think you are looking at a pathological example. Try something more complex (results shown for R2012b on OSX):

(repeated addition)

>> 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

(repeated multiplication)

>> 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

One thing to remember is that optimizations are applied differently whether you are running from the command line or a saved M-function.

Here is a test of my own:

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

The results on my Windows machine running R2013a:

>> 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)

Note that the results fluctuate a little bit with different runs where the order of calls is shuffled. So take the timings with a grain of salt.

My conclusion: doesn't matter in terms of speed if you use 1i or 1*i.


One interesting difference is that if you also have a variable in the function scope where you also use it as the imaginary unit, MATLAB throws an error:

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.

To see the error, change the above test3 function into:

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

i.e, the variable i was used as both a function and a variable in the same local scope.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!