bodhitreechen
2010-07-03, 00:57
本帖想建议大家:
对于大量调用的函数应尽量避免使用inline。
下面是一个测试程序,其运行结果表明,大量调用的
函数用inline实现是不明智的。
测试程序:
function [t1,t2,t3,t4]=test(t)
fhu = @(s)[[s(2)+s(1)^3,s(1)*s(2)+s(1)^2];[s(1)*s(2),s(2)^2]];
fhi = @(s1,s2)[[s1^3+s2,s1^2+s2*s1];[s1*s2,s2^2]];
syms x1 x2 ;
x=[x1, x2];
fu=inline('[[x(2)+x(1)^3,x(1)*x(2)+x(1)^2];[x(1)*x(2),x(2)^2]]','x');
fi=inline('[[x1^3+x2,x1^2+x2*x1];[x1*x2,x2^2]]','x1','x2');
t1=0;
t2=0;
t3=0;
t4=0;
for it=1:t
switch ceil(4*rand(1))
case 1
tic
t= fhu([rand(1),rand(1)]);
t1=toc+t1;
case 2
tic
t= fhi(rand(1),rand(1));
t2=toc+t2;
case 3
tic
t= fu([rand(1),rand(1)]);
t3=toc+t3;
case 4
tic
t= fi(rand(1),rand(1));
t4=toc+t4;
end
end
运行结果:
[t1,t2,t3,t4]=test(100000)
t1 =
1.7473
t2 =
1.6115
t3 =
20.3300
t4 =
17.8167
对于大量调用的函数应尽量避免使用inline。
下面是一个测试程序,其运行结果表明,大量调用的
函数用inline实现是不明智的。
测试程序:
function [t1,t2,t3,t4]=test(t)
fhu = @(s)[[s(2)+s(1)^3,s(1)*s(2)+s(1)^2];[s(1)*s(2),s(2)^2]];
fhi = @(s1,s2)[[s1^3+s2,s1^2+s2*s1];[s1*s2,s2^2]];
syms x1 x2 ;
x=[x1, x2];
fu=inline('[[x(2)+x(1)^3,x(1)*x(2)+x(1)^2];[x(1)*x(2),x(2)^2]]','x');
fi=inline('[[x1^3+x2,x1^2+x2*x1];[x1*x2,x2^2]]','x1','x2');
t1=0;
t2=0;
t3=0;
t4=0;
for it=1:t
switch ceil(4*rand(1))
case 1
tic
t= fhu([rand(1),rand(1)]);
t1=toc+t1;
case 2
tic
t= fhi(rand(1),rand(1));
t2=toc+t2;
case 3
tic
t= fu([rand(1),rand(1)]);
t3=toc+t3;
case 4
tic
t= fi(rand(1),rand(1));
t4=toc+t4;
end
end
运行结果:
[t1,t2,t3,t4]=test(100000)
t1 =
1.7473
t2 =
1.6115
t3 =
20.3300
t4 =
17.8167