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

2014-11-24 12:41:19 · 作者: · 浏览: 5
/(1.0+abs(g));
n=n+n; g0=g; t1=t2; h=0.5*h;
end
end

%file f2s.m
function [y0,y1]=f2s(x)
y0=-sqrt(1.0-x*x);
y1=-y0;
end

%file f2f.m
function c=f2f(x,y)
c=exp(x*x+y*y);
end

%%%%%%%%%%%%%%%%

>> tic
for i=1:100
a=fsim2(0,1,0.0001,@f2s,@f2f);
end
a
toc

a =

2.6989

Elapsed time is 1.267014 seconds.

--------

Lu代码:

simp1(x,eps,fsim2s,fsim2f : n,i,y0,y1,h,d,t1,yy,t2,g,ep,g0)=
{
n=1,
fsim2s(x,&y0,&y1),
h=0.5*(y1-y0),
d=abs(h*2.0e-06),
t1=h*(fsim2f(x,y0)+fsim2f(x,y1)),
ep=1.0+eps, g0=1.0e+35,
while {((ep>=eps)&(abs(h)>d))|(n<16),
yy=y0-h,
t2=0.5*t1,
i=1, while{i<=n,
yy=yy+2.0*h,
t2=t2+h*fsim2f(x,yy),
i++
},
g=(4.0*t2-t1)/3.0,
ep=abs(g-g0)/(1.0+abs(g)),
n=n+n, g0=g, t1=t2, h=0.5*h
},
g
};

fsim2(a,b,eps,fsim2s,fsim2f : n,j,h,d,s1,s2,t1,x,t2,g,s,s0,ep)=
{
n=1, h=0.5*(b-a),
d=abs((b-a)*1.0e-06),
s1=simp1(a,eps,fsim2s,fsim2f), s2=simp1(b,eps,fsim2s,fsim2f),
t1=h*(s1+s2),
s0=1.0e+35, ep=1.0+eps,
while {((ep>=eps)&(abs(h)>d))|(n<16),
x=a-h, t2=0.5*t1,
j=1, while{j<=n,
x=x+2.0*h,
g=simp1(x,eps,fsim2s,fsim2f),
t2=t2+h*g,
j++
},
s=(4.0*t2-t1)/3.0,
ep=abs(s-s0)/(1.0+abs(s)),
n=n+n, s0=s, t1=t2, h=h*0.5
},
s
};

//////////////////

f2s(x,y0,y1)=
{
y0=-sqrt(1.0-x*x),
y1=-y0
};
f2f(x,y)=exp(x*x+y*y);

main(:t0,i,a)=
t0=clock(),
i=0, while{i<100, a=fsim2(0,1,0.0001,HFor("f2s"),HFor("f2f")), i++},
o{"a=",a,", time=",[clock()-t0]/1000.};

Lu运行结果:

a=2.6989250006243033, time=0.719

--------

本例matlab与Lu耗时之比为1.267014 :0.719。

6 Matlab与Lu动态内存管理效率

matlab 2009a代码:

clear all
tic
s=0;
for k=1:1000
for i=1:1000
a={2,2,2,2};
s=s+a{1};
end
end
s
toc

s =

2000000

Elapsed time is 5.030599 seconds.

Lu代码:

main(:a,t0,s,k,i)=
t0=clock(),
s=0,
k=0, while{k<1000,
i=0, while{i<1000, a=lu(2,2,2,2), s=s+a[1], i++},
k++
},
o{"s=",s,", time=",[clock()-t0]/1000.};

Lu运行结果:

s=2000000, time=0.703

7 Lu与VC动态数组存取效率测试

VC源程序:

#include "stdafx.h"
#include
#include
#include
#include

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
clock_t old,now;
__int64 i,k;
__int64 *p;
char ch;

old=clock();
for(i=0,k=0;i<=1000000;i++)
{
p=new __int64[5];
p[3]=i; k=k+p[3];
delete[] p;
}
now=clock();
cout<<"vc++:"< cout<<" 运行时间:"< cin>>ch;

return 0;
}

VC运行结果:

vc++:500000500000 运行时间:218 即: 0.218秒

Lu源程序:

(:i,k,p,t)=
{ t=clock(),i=0,k=0,
(i<=1000000).while
{
p=new[ints,5],
A[p,3]=i, k=k+A[p,3],
del[p], //实际上在这里不需要使用del函数,去掉此语句将更快
i++
},
o{"结果=",k,", 时间=",[clock()-t]/1000.,"秒。\r\n"}
};

Lu运行结果:

结果=500000500000, 时间=0.86秒。

在该例子中,Lu的效率为VC的0.86/0.218=3.95分之一。若将new和del这两个函数移到循环体的外边,Lu的效率如下:

VC源程序:

#include "stdafx.h"
#include
#include
#include
#include


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
clock_t old,now;
__int64 i,k;
__int64 *p;
char ch;

old=clock(); p=new __int64 [5];
for(i=0,k=0;i<=20000000;i++)
{
p[3]=i; k=k+p[3];
}
delete[] p; now=clock();
cout<<"vc++:"< cout<<" 运行时间:"< cin>>ch;

return 0;
}

VC运行结果:

vc++:200000010000000 运行时间:125 即: 0.125秒

Lu源程序:

(:i,k,p,t)=
{ t=clock(),p=new[ints,5],i=0,k=0,
(i<=20000000).while
{
A[p,3]=i, k=k+A[p,3],
i++
},
del[p], /