在Matlab中更多地使用i和j作为变量:speed。

时间:2021-07-31 00:14:30

The Matlab documentation says that

Matlab文档说明。

For speed and improved robustness, you can replace complex i and j by 1i. For example, instead of using

对于速度和改进的鲁棒性,可以用1i替换复杂i和j。例如,不要使用。

a = i;

use

使用

a = 1i;

The robustness part is clear, as there might be variables called i or j. However, as for speed, I have made a simple test in Matlab 2010b and I obtain results which seem to contradict the claim:

稳健性部分是明确的,因为可能有变量i或j。但是,对于速度,我在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.

Any ideas? Could it be a version-related issue?

什么好主意吗?这可能是一个版本相关的问题吗?

After comments by @TryHard and @horchler, I have tried assigning other values to the variable a, with these results:

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

Increasing order of elapsed time:

增加运行时间顺序:

"i" < "1i" < "1*i" (trend "A")

"i" < "1i" < "1*i"(趋势A)

"2i" < "2*1i" < "2*i" (trend "B")

"2i" < "2*1i" < "2*i"(趋势B)

"1+1i" < "1+i" < "1+1*i" (trend "A")

"1+1i" < "1+i" < 1+1*i"(趋势A)

"2+2i" < "2+2*1i" < "2+2*i" (trend "B")

"2+2i" < "2+2*1i" < 2+2*i"(趋势B)

2 个解决方案

#1


9  

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

我想你正在看一个病理的例子。尝试一些更复杂的(在OSX上显示的R2012b):

(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

#2


5  

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

需要记住的一点是,无论您是从命令行运行还是从一个已保存的m函数中运行,优化都是不同的。

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:

我的Windows机器运行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.

我的结论是:如果你使用1i或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:

一个有趣的区别是,如果你在函数范围内有一个变量你也可以用它作为假想的单位,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.

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

要查看错误,请将上面的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.e, the variable i was used as both a function and a variable in the same local scope.

我。在相同的局部范围内,变量i被用作函数和变量。

#1


9  

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

我想你正在看一个病理的例子。尝试一些更复杂的(在OSX上显示的R2012b):

(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

#2


5  

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

需要记住的一点是,无论您是从命令行运行还是从一个已保存的m函数中运行,优化都是不同的。

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:

我的Windows机器运行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.

我的结论是:如果你使用1i或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:

一个有趣的区别是,如果你在函数范围内有一个变量你也可以用它作为假想的单位,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.

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

要查看错误,请将上面的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.e, the variable i was used as both a function and a variable in the same local scope.

我。在相同的局部范围内,变量i被用作函数和变量。