Lu与C/C++、Forcal、MATLAB、Python、Lua等各种语言的速度比较(四)

2014-11-24 12:41:19 · 作者: · 浏览: 4
in[]=L"mymain (:i,x,y,old,now) = {\r\n old=clock(),\r\n y = -39,\r\n while{ y < 39,\r\n o(\"\r\n\"),\r\n x = -39,\r\n while{ x < 39,\r\n i = mandelbrot(x/40.0, y/40.0),\r\n which{ i==0,\r\n o(\"*\"),\r\n o(\" \")\r\n },\r\n x++\r\n },\r\n y++\r\n },\r\n now=clock(),\r\n o[\"\r\nLu Elapsed \",(now-old)/1000.0]\r\n}"; //字符串表达式

if(!InitLu()) return; //初始化Lu
InsertKey("\0\0\0\0",4,luPubKey_User,LuMessage,NULL,NULL,1,v); //使Lu运行时可输出函数信息

ErrCode1=LuCom(mandelbrot,0,0,0,hFor,nPara,pPara,ErrBegin,ErrEnd); //编译表达式
ErrCode2=LuCom(mymain,0,0,0,hFor,nPara,pPara,ErrBegin,ErrEnd); //编译表达式
if(ErrCode1 || ErrCode2)
{
cout<<"表达式有错误!错误代码:"< }
else
{
LuCal(hFor,pPara); //计算表达式的值
}
FreeLu(); //释放Lu
}

运行时输出的图形与C程序相同,运行时间为:

Lu Elapsed 2.063

从运行结果可看出,Lu用的时间与c++相比,为2.063:0.25=8.252:1。

9 Lu与Python比较的测试例子

Python程序:

Python code>>> def f(k):
n=0
i=0
while i <=k:
n=n+i
i=i+1
return n

>>> f(100000000)
5000000050000000

耗时约32秒(用秒表测量)。

Lu程序:

f(k:i,n)= i=0,n=0, while{i<=k, n=n+i++}, n;
(:t)= t=clock(), o{"f(100000000)=",f(100000000),", time=",[clock()-t]/1000.};

Lu运行结果:

f(100000000)=5000000050000000, time=14.797

======

另一个测试程序:

Python 程序:

Python code>>> def f(x,y):
return math.cos(1-math.sin(1.2*(x+0.1)**(y/2-x)+math.cos(1-math.sin(1.2*(x+0.2)**(y/3-x))))-math.cos(1-math.sin(1.2*(x+0.3)**(y/4-x)))-math.cos(1-math.sin(1.2*(x+0.4)**(y/5-x)+math.cos(1-math.sin(1.2*(x+0.5)**(y/6-x))))-math.cos(1-math.sin(1.2*(x+0.6)**(y/7-x)))))

>>> def g(m,n):
x=0
i=0
while i <=m:
j=0
while j <=n:
x=x+f(i,j)
j=j+0.01
i=i+0.01
return x

>>> g(12.3,7.89)
404005.29176341009

Python运行约10秒(用秒表测量)。

Lu程序:

f(x,y)=cos(1-sin(1.2*(x+0.1)^(y/2-x)+cos(1-sin(1.2*(x+0.2)^(y/3-x))))-cos(1-sin(1.2*(x+0.3)^(y/4-x)))-cos(1-sin(1.2*(x+0.4)^(y/5-x)+cos(1-sin(1.2*(x+0.5)^(y/6-x))))-cos(1-sin(1.2*(x+0.6)^(y/7-x)))));
g(m,n:i,j,x)=
x=0,i=0,
(i<=m).while{
j=0,
(j<=n).while{
x=x+f(i,j),
j=j+0.01
},
i=i+0.01
},
x;
main(:t)= t=clock(), o{"g(12.3,7.89)=",g(12.3,7.89),", time=",[clock()-t]/1000.};

Lu结果:

g(12.3,7.89)=404005.29176341009, time=3.43

版权所有 Lu程序设计 2002-2010,保留所有权利
E-mail: Lu@sina.com QQ:630715621
最近更新:2011年10月19日