lua 和 luajit 性能对比测试

lua 和 luajit 性能对比测试

测试脚本 b.lua

local i = 0

while 1 do
    i = i+1
    if i > 10000000 then
        print(i)
        break
    end
end

测试 C 程序

#include 

int main() {
    int i = 0;

    while (1) {
        i = i+1;
        if (i > 10000000) {
            printf("%d\n", i);
            break;
        }
    }
    return 0;
}

gcc -g -o b b.c

测试结果

[root@123 luaffi]# time ./b
10000001

real    0m0.025s
user    0m0.025s
sys 0m0.001s
[root@123 luaffi]# time ./b
10000001

real    0m0.025s
user    0m0.025s
sys 0m0.001s
[root@123 luaffi]# time ./b
10000001

real    0m0.025s
user    0m0.025s
sys 0m0.001s
[root@123 luaffi]# time ./b
10000001

real    0m0.026s
user    0m0.025s
sys 0m0.001s
[root@123 luaffi]# time ./b
10000001

real    0m0.025s
user    0m0.024s
sys 0m0.002s
[root@123 luaffi]# time luajit b.lua
10000001

real    0m0.017s
user    0m0.017s
sys 0m0.000s
[root@123 luaffi]# time luajit b.lua
10000001

real    0m0.018s
user    0m0.016s
sys 0m0.001s
[root@123 luaffi]# time luajit b.lua
10000001

real    0m0.018s
user    0m0.017s
sys 0m0.001s
[root@123 luaffi]# time lua b.lua
10000001

real    0m0.150s
user    0m0.149s
sys 0m0.001s
[root@123 luaffi]# time lua b.lua
10000001

real    0m0.144s
user    0m0.143s
sys 0m0.001s
[root@123 luaffi]# time lua b.lua
10000001

real    0m0.145s
user    0m0.144s
sys 0m0.000s

结论:主要在用户态执行的 Lua 程序,Luajit 的运行速度远高于 Lua 的运行速度,这里使用的测试程序 Luajit 大约是 Lua 的 8 倍左右。在某种程度上可以达到和 C 程序差不多的速度。

你可能感兴趣的:(lua,性能,Lua)