Java教程

lua5.4.2(使用Lua54程序) 与 Lu2.0(使用OpenLu程序)速度比较

本文主要是介绍lua5.4.2(使用Lua54程序) 与 Lu2.0(使用OpenLu程序)速度比较,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

lua5.4.2(使用Lua54程序) 与 Lu2.0(使用OpenLu程序)速度比较

lua5.4.2(Lua54程序):请从Lua官方网站下载。
Lu2.0(OpenLu程序):http://www.forcal.net/xiazai/lu2/openlu64.zip

从网上看到,C/C++调用Lua脚本函数的效率较低,似乎只有Lua脚本内部调用函数效率的几分之一。在这个方面Lu脚本有明显的优势,C/C++调用Lu脚本函数的效率与Lu脚本内部调用函数效率几乎相同,如果说有差别,应是C/C++调用Lu脚本函数的效率要高一点。Lu脚本更适合于宿主语言与脚本之间的频繁穿越。

下面仅比较Lua脚本与Lu脚本本身的效率,结果表明,平均来说,Lua脚本比Lu脚本速度略快,但差别不大(协程尚未比较,仅给出了Lu协程的例子)。

例子1:数值计算

Lua代码:

function z()
    local t = os.clock()
    local mcos = math.cos
    local msin = math.sin

    local x=0
    local y=0
    local z=0
    for x=0,1,0.0011 do
        for y=1,2,0.0011 do
            z=z+mcos(1-msin(1.2*(x+0.1)^(y/2-x)+mcos(1-msin(1.2*(x+0.2)^(y/3-x))))-mcos(1-
msin(1.2*(x+0.3)^(y/4-x)))-mcos(1-msin(1.2*(x+0.4)^(y/5-x)+mcos(1-msin(1.2*(x+0.5)^(y/6-x))))-
mcos(1-msin(1.2*(x+0.6)^(y/7-x))))) 
        end

    end
    io.write(z)

    io.write(string.format(" Time Elapsed %f\n", os.clock() - t))
end
z()

Lua运行结果:

19160.536601703 Time Elapsed 1.67

Lu代码1:

mvar:    //使用未定义的模块变量。变量未定义就可以使用,容易隐藏不可预知的错误,故后面不再使用这种方法

t=clock(),  z=0.0,  x=0.0,
while{    x<=1.0,
    y=1.0,
    while{    y<=2.0,
        z=z+cos(1.0-sin(1.2*(x+0.1)^(y/2.0-x)+cos(1.0-sin(1.2*(x+0.2)^(y/3.0-x))))-cos(1.0-sin(1.2*(x+0.3)^(y/4.0-x)))-cos(1.0-sin(1.2*(x+0.4)^(y/5.0-x)+cos(1.0-sin(1.2*(x+0.5)^(y/6.0-x))))-cos(1.0-sin(1.2*(x+0.6)^(y/7.0-x))))),
        y=y+0.0011
    },
    x=x+0.0011
},
o{"z=",z,",耗时约",[clock()-t]/1000.0,"秒。\r\n"};;

Lu运行结果:

z=19160.536601703152,耗时约0.875秒。

Lu代码2:

//函数main中,x, y, z, t都是预先声明的自动变量,如果这些变量放在两个冒号后,就成为模块变量,执行效率与上例相同。模块变量比自动变量效率稍高
main(: x, y, z, t)=    //函数名main不是必须的,后面将予以省略
{
    t=clock(),  z=0.0,  x=0.0,
    while{    x<=1.0,
        y=1.0,
        while{    y<=2.0,
            z=z+cos(1.0-sin(1.2*(x+0.1)^(y/2.0-x)+cos(1.0-sin(1.2*(x+0.2)^(y/3.0-x))))-cos(1.0-sin(1.2*(x+0.3)^(y/4.0-x)))-cos(1.0-sin(1.2*(x+0.4)^(y/5.0-x)+cos(1.0-sin(1.2*(x+0.5)^(y/6.0-x))))-cos(1.0-sin(1.2*(x+0.6)^(y/7.0-x))))),
            y=y+0.0011
        },
        x=x+0.0011
    },
    o{"z=",z,",耗时约",[clock()-t]/1000.0,"秒。\r\n"}
};

Lu运行结果:

z=19160.536601703152,耗时约0.962秒。

例子2:计算π

Lua代码:

function z()
    local t = os.clock()
    local pi=0.0
    local MaxK=10000000
    local i=0
    while(i<MaxK)
    do 
        pi = pi + 1.0 / ((4 * i + 1) * (2 * i + 1) * (i + 1))
        i = i + 1
    end
    pi = pi * 3
    io.write(pi)
    io.write(string.format(" Time Elapsed %f\n", os.clock() - t))
end
z()

Lua运行结果:

3.1415926535536 Time Elapsed 0.731000

Lu代码:

(: pi, t, i, MaxK) =
t= clock(),
pi=0.0,  MaxK=10000000,  i=0,
while{    i<MaxK,
    pi = pi + 1.0 / ((4 * i + 1) * (2 * i + 1) * (i + 1)),
    i++
},
pi = pi*3,
o{pi,"    time=",[clock()-t]/1000.0, "\r\n"};;

Lu运行结果:

3.1415926535535581    time=1.22

例子3:函数调用

Lua代码:

function f(x, y)
    return x+y;
end
function z()
    local t = os.clock()
    local pi=0.0
    local MaxK=1000000
    local i=0
    while(i<MaxK)
    do 
        pi = pi + 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))
        i = i + 1
    end
    pi = pi * 3
    io.write(pi)
    io.write(string.format(" Time Elapsed %f\n", os.clock() - t))
end
z()

Lua运行结果:

50.265482456022 Time Elapsed 3.1780

Lu代码:

f(x, y) = x + y;
(:pi, t, i, MaxK) =
t= clock(),
pi=0.0,
MaxK=1000000,
i=0,
while{    i<MaxK,
    pi = pi + 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1))+ 1.0 / (f(4 * i , 1) *f (2 * i , 1) *f (i , 1)),
    i++
},
pi = pi*3,
o{pi,"    time=",[clock()-t]/1000.0, "\r\n"};;

Lu运行结果:

50.265482456021743    time=3.16

例子4:函数返回多个参数

Lua代码:

function f(x, y)
    return x+y;
end
function g(x)
    return 4*x, 2*x , x;
end
function h(x)
    return 4*x, 2*x , -6*x;
end
function z()
    local t = os.clock()
    local pi=0.0
    local MaxK=1000000
    local i=0
    local a
    local b
    local c
    local d
    local e
    local m
    while(i<MaxK)
    do 
        a,b,c=g(i)
        d,e,m=h(i)
        a,b,c=g(i)
        d,e,m=h(i)
        a,b,c=g(i)
        d,e,m=h(i)
        pi = pi + 1.0 / (f(a , 1) *f (b , 1) *f (c , 1)) +d +e +m
        i = i + 1
    end
    pi = pi * 3
    io.write(pi)
    io.write(string.format(" Time Elapsed %f\n", os.clock() - t))
end
z()

Lua运行结果:

3.1415926404297 Time Elapsed 0.70

Lu代码:

f(x, y) = x+y;
g(x, a, b, c)= a=4*x,  b=2*x , c=x;
h(x, a, b, c)= a=4*x,  b=2*x , c=-6*x;
(:pi, t, i, MaxK, a, b, c, d, e, m)=
t= clock(),
pi=0.0,
MaxK=1000000,
i=0,
while{    i<MaxK,
    g(i, &a, &b, &c),  h(i, &d, &e, &m),
    g(i, &a ,&b, &c),  h(i, &d, &e, &m),
    g(i, &a, &b, &c),  h(i, &d, &e, &m),
    pi = pi + 1.0 / (f(a , 1) *f (b , 1) *f (c , 1)) +d +e +m,
    i++
},
pi = pi*3,
o{pi,"    time=",[clock()-t]/1000.0, "\r\n"};;

Lu运行结果:

3.1415926404297352    time=1.10

例子5:递归调用

Lua代码:

function fib(n)
    if n<2 then
        return n
    else
        return fib(n-2)+fib(n-1)
    end
end
function z( )
    local N=40
    local t=os.clock()
    print("计算结果:"..fib(N).."  计算"..N.."个斐波那契数列耗时:"..(os.clock()-t).."秒")
end
z( )

Lua运行结果:

计算结果:102334155  计算40个斐波那契数列耗时:14.433秒

Lu代码:

SetStackMax[1000];;
fib(n)= which{ n<2 :  n;  fib(n-2)+fib(n-1)};
(:n,t) = n=40, t=clock(), o{"计算结果:", fib(n), "  计算", n, "个斐波那契数列耗时:",(clock()-t)/1000.0, "秒\r\n"};

Lu运行结果:

计算结果:102334155  计算40个斐波那契数列耗时:21.505秒

例子6:动态对象 Lua table(表) 及 Lu lu(表)

Lua代码:

function z()
local t=os.clock()
local s=0
local i
for i=1, 1000000 do
    local a1 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b1 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a2 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b2 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a3 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b3 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a4 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b4 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a5 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b5 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a6 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b6 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a7 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b7 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a8 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b8 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a9 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b9 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a10 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b10 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a11 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b11 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local a12 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    local b12 = {1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}
    s=s +a1[3]+b1[3]+a2[3]+b2[3]+a3[3]+b3[3]+a4[3]+b4[3]+a5[3]+b5[3]+a6[3]+b6[3]+a7[3]+b7[3]+a8[3]+b8[3]+a9[3]+b9[3]+a10[3]+b10[3]+a11[3]+b11[3]+a12[3]+b12[3]
end
print("s= "..s.."  耗时:"..(os.clock()-t).."秒")
end
z()

Lua运行结果:

s= 72000000  耗时:7.781秒

Lu代码:

(:i, a, b, s, t, a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7, b7, a8, b8, a9, b9, a10, b10, a11, b11, a12, b12) =
t=clock( ),  s=0,
i=0, while{    i<1000000,
    a1=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b1=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a2=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b2=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a3=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b3=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a4=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b4=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a5=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b5=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a6=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b6=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a7=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b7=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a8=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b8=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a9=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b9=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a10=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b10=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a11=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b11=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    a12=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0}, b12=lu{1, 2, 3,4,5,6,7,8,9,0,1, 2, 3,4,5,6,7,8,9,0},
    s=s +a1[2]+b1[2]+a2[2]+b2[2]+a3[2]+b3[2]+a4[2]+b4[2]+a5[2]+b5[2]+a6[2]+b6[2]+a7[2]+b7[2]+a8[2]+b8[2]+a9[2]+b9[2]+a10[2]+b10[2]+a11[2]+b11[2]+a12[2]+b12[2],
     i++
},
o{"s= ",s, "耗时:",(clock()-t)/1000.0, "秒\r\n"};

Lu运行结果:

s= 72000000耗时:8.975秒

例子6:协程

Lu代码:

f1(x : a : c1)= a=0, c1=0, while{true, c1++, a = a + x, yield(a)};  //自动变量a对自变量x的值进行累加,模块变量c1记录f1的执行次数
f2(x : a : c2)= a=0, c2=0, while{true, c2++, a = a + x, yield(a)};  //自动变量a对自变量x的值进行累加,模块变量c2记录f2的执行次数
f3(x : a : c3)= a=0, c3=0, while{true, c3++, a = a + x, yield(a)};  //自动变量a对自变量x的值进行累加,模块变量c3记录f3的执行次数

main(:i, t : c1, c2, c3) =
{

    t=clock(),  //获取时间

    i=-1, while{++i<10000000, which{i%5 : f1[i]; i%3 : f2[i]; f3[i]}},    //i%5不等于0时调用f1;i%3不等于0时调用f2;否则调用f3

    o{"协程f1的终值=",f1[0],",协程f2的终值=",f2[0],",协程f3的终值=",f3[0],"\r\n"},

    o{"协程总执行次数c1+c2+c3=",c1+c2+c3,",c1=",c1,",c2=",c2,",c3=",c3,",耗时约",[clock()-t]/1000.0,"秒。\r\n"}
};;

Lu运行结果:

协程f1的终值=40000000000000,协程f2的终值=6666663333335,协程f3的终值=3333331666665
协程总执行次数c1+c2+c3=10000003,c1=8000001,c2=1333334,c3=666668,耗时约1.33秒。
 

这篇关于lua5.4.2(使用Lua54程序) 与 Lu2.0(使用OpenLu程序)速度比较的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!