Bir şey optimizasyonlar veya makineden uygulanır olmasıdır komut satırından veya kayıtlı bir M-işlevinden kaçıyorsunuz. R2013a çalıştıran Windows makinemde
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
sonuç: Burada
kendime ait bir testtir sonuçların farklı pistlere sahip biraz dalgalanma olduğunu
>> 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)
Not nerede sırası Aramalar karıştırıldı. Bu yüzden zamanlamaları bir tuz parçasıyla al.
Sonucum: 1i
veya 1*i
'u kullanırsanız hız açısından önemli değil.
ilginç bir fark da ayrıca hayali birimi olarak kullanmak fonksiyonu kapsamında bir değişken varsa, MATLAB bir hata atar şudur:
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.
hatayı görmek için değişiklik yukardaki test3
fonksiyonu: yani
function a = test3
a = zeros(1e7,1);
for n=1:1e7
a(n) = 2 + 2*i;
end
i = rand(); %// added this line!
end
değişken i
bir fonksiyonu ve aynı yerel kapsamında bir değişken hem de kullanılmıştır.
Daha genel olan '2 * i 'vs.' 2i' (veya hatta 2 * 1i') hakkında ne dersiniz? – horchler
Ben XP ile rinky dink dizüstü bilgisayarda R14 ile bir x 5 iyileşme görüyorum. –
Her döngüden önce "tümünü temizle" uygulayabilirim? –