本文介绍了有关在Matlab中将i和j用作变量的更多信息:speed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matlab文档表示

a = i;

使用

a = 1i;

稳健性部分很明确,因为可能会有名为ij 的变量.但是,关于 speed ,我在Matlab 2010b中做了一个简单的测试,得出的结果似乎与主张相矛盾:

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:

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

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

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

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

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

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

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

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

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

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

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

推荐答案

我认为您正在查看一个病理示例.尝试更复杂的操作(在OSX上显示R2012b的结果):

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

(重复添加)

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

这篇关于有关在Matlab中将i和j用作变量的更多信息:speed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:03