完整测试代码如下:
测试目标主要是看看c与lua之前互调的性能如何。
测试结论:c调用lua与lua调用c基本只有一个数量级的性能损失。
从c中调用lua,使用缓存ref的方式,可以提升30%的性能。
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include "skynet.h"
// 取当前毫秒值
static int
ltime(lua_State *L) {
struct timeval tv;
gettimeofday(&tv,NULL);
lua_pushinteger(L,tv.tv_sec*1000+tv.tv_usec/1000);
return 1;
}
// 供lua调用,内部无业务逻辑
static int
ltest1(lua_State *L) {
return 0;
}
// 供lua调用, 内部int相加并返回
static int
ltest2(lua_State *L) {
int a1=luaL_checkinteger(L,1);
int a2=luaL_checkinteger(L,2);
lua_pushinteger(L,a1+a2);
return 1;
}
// 内部调用lua中方法
static int
ltest3(lua_State *L) {
lua_getglobal(L,"global_test_func1");
lua_call(L,0,0);
return 0;
}
// 内部调用lua中方法,并取得lua中返回值
static int
ltest4(lua_State *L) {
lua_getglobal(L,"global_test_func2");
lua_pushinteger(L,1);
lua_pushinteger(L,2);
lua_call(L,2,1);
int sum = (int)lua_tointeger(L, -1);
lua_pop(L, 1);
lua_pushinteger(L,sum);
return 1;
}
// 内部调用lua方法10000次
static int
ltest5(lua_State *L) {
int sum=0;
for(int m=0;m<10000000;m++){
lua_getglobal(L,"global_test_func2");
lua_pushinteger(L,1);
lua_pushinteger(L,2);
lua_call(L,2,1);
sum=(int)lua_tointeger(L, -1);
lua_pop(L, 1);
}
lua_pushinteger(L,sum);
return 1;
}
// 和test5一起对比
static int
ltest6(lua_State *L) {
for(int m=0;m<10000000;m++){
lua_getglobal(L,"global_test_func2");
lua_pushinteger(L,1);
lua_pushinteger(L,2);
lua_call(L,2,1);
lua_pop(L, 1);
}
lua_pushinteger(L,3);
return 1;
}
// 和test5一起做对比
static int
ltest7(lua_State *L) {
int func2_handler;
lua_getglobal(L,"global_test_func2");
func2_handler=luaL_ref(L,LUA_REGISTRYINDEX);
for(int m=0;m<10000000;m++){
lua_rawgeti(L,LUA_REGISTRYINDEX,func2_handler);
lua_pushinteger(L,1);
lua_pushinteger(L,2);
lua_call(L,2,1);
lua_pop(L,1);
}
//luaL_unref(L,LUA_REGISTRYINDEX);
lua_pushinteger(L,3);
return 1;
}
// 和test5一起做对比一下
int add(int a,int b){
return a+b;
}
static int
ltest8(lua_State *L) {
for(int m=0;m<10000000;m++){
add(1,2);
}
return 0;
}
// full userdata
// light userdata
int
luaopen_perf(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "time", ltime },
{ "test1", ltest1 }, // 从lua中直接调用c,内部无逻辑
{ "test2", ltest2 },
{ "test3", ltest3 },
{ "test4", ltest4 },
{ "test5", ltest5 },
{ "test6", ltest6 },
{ "test7", ltest7 },
{ "test8", ltest8 },
{ NULL, NULL}
};
luaL_newlib(L,l);
return 1;
}
lua代码:
require "common"
local perf = require "perf"
function global_test_func1()
-- print("global_test_func1")
end
function global_test_func2(a,b)
return a+b
end
local t1
local t2
t1=perf.time()
for i=1,10000000 do
perf.test1()
end
t2=perf.time()
print("test1:"..(t2-t1))
t1=perf.time()
for i=1,10000000 do
perf.test2(1,2)
end
t2=perf.time()
print("test2:"..(t2-t1))
t1=perf.time()
for i=1,10000000 do
perf.test3()
end
t2=perf.time()
print("test3:"..(t2-t1))
t1=perf.time()
for i=1,10000000 do
perf.test4()
end
t2=perf.time()
print("test4:"..(t2-t1))
t1=perf.time()
perf.test5()
t2=perf.time()
print("test5:"..(t2-t1))
t1=perf.time()
perf.test6()
t2=perf.time()
print("test6:"..(t2-t1))
t1=perf.time()
perf.test7()
t2=perf.time()
print("test7:"..(t2-t1))
t1=perf.time()
perf.test8()
t2=perf.time()
print("test8:"..(t2-t1))
运行结果:
test1:470 test2:777 test3:1619 test4:1941 test5:1490 test6:1437 test7:675 test8:40
0 条评论。