C语言中调用lua 脚本执行的效率测试

来源:互联网 发布:linux部署snmp 编辑:程序博客网 时间:2024/04/30 09:00

// mytest.cpp : Defines the entry point for the console application.//

#include "stdafx.h"

#include <stdio.h>#include <windows.h>

extern "C" { #include "lua.h"#include "lualib.h"#include "lauxlib.h"}/* Lua解释器指针 */

//function for Lua to call//return a integer array to the script//C里面的函数由lua脚本调用static int getarr (lua_State *Lo) {    lua_newtable(Lo);//create table

    lua_pushnumber(Lo,11);//push the value    lua_rawseti(Lo,-2,1);//set t[1]=v

    lua_pushnumber(Lo,22);        lua_rawseti(Lo,-2,2);

    lua_pushnumber(Lo,33);        lua_rawseti(Lo,-2,3);

    lua_pushnumber(Lo,44);        lua_rawseti(Lo,-2,4);        return 1;}

//lua 脚本里面的函数由C调用int use_lua_add (lua_State *L,const char * func_name ,int x, int y ){ int sum; /* 通过名字得到Lua函数 */ lua_getglobal(L, func_name); /* 第一个参数 */ lua_pushnumber(L, x); /* 第二个参数 */ lua_pushnumber(L, y);  /* 调用函数,告知有两个参数,一个返回值 */ lua_call(L, 2, 1); /* 得到结果 */ sum = (int)lua_tointeger(L, -1); lua_pop(L, 1); return sum;}

int use_lua(){ int i,sum = 0; unsigned int tStart,tStop;    lua_State *L = lua_open();   /* opens Lua */    luaopen_base(L);             /* opens the basic library */    luaopen_table(L);            /* opens the table library */        luaopen_string(L);           /* opens the string lib. */    luaopen_math(L);             /* opens the math lib. */   

    //luaL_openlibs(L); lua_pushcfunction(L, getarr); // Register a function    lua_setglobal(L, "getarr");    if (luaL_dofile(L, "c_call.lua"))//Load the script file and Run it    {        printf("run script failed/n");    }    else    {  printf("The sum is %d./n",use_lua_add(L,"add",100,23));        lua_getglobal(L, "result"); //Get the global variant in Lua script        if(lua_isnumber(L,-1))        {            printf("The result of the Lua script is %d/n",lua_tointeger(L,-1));//lua_tonumber(L,-1));   lua_pop(L, 1);        }  sum = 0;  tStart = GetTickCount();  for(i = 0;i< 10000000;i++)  {   sum +=  use_lua_add(L,"add",1,1);  }  tStop = GetTickCount();          printf("The time length of add is %d./nThe sum is %u./n",tStop - tStart,sum);

  sum = 0;  tStart = GetTickCount();        sum =  use_lua_add(L,"loop_add",1,1);  tStop = GetTickCount();        printf("The time length of loop_add is %d./nThe sum is %u./n",tStop - tStart,sum);

  sum = 0;  tStart = GetTickCount();  for(i = 0;i< 10000000;i++)  {   sum += 1 + 1;  }  tStop = GetTickCount();          printf("The time length is %d./nThe sum is %u./n",tStop - tStart,sum);

    }           lua_close(L);         return 0;}

void execute_lua_shell(){ lua_State* L; /* 初始化Lua */  L = lua_open();  /* 载入Lua基本库 */  luaL_openlibs(L);  /* 运行脚本 */  luaL_dofile(L, "test.lua"); /* 清除Lua */  lua_close(L); }

int main ( int argc, char *argv[] ){ use_lua();

 //execute_lua_shell();

 return 0;}

//c_call.lua

function loop_add(a,b)  local sum = 0  for i=1,10000000 do   sum = sum + a + b  end  return sumend

function add(a,b)  return a + bend

 

结果:

11223344The sum is 123.The result of the Lua script is 189The time length of add is 2203.The sum is 20000000.The time length of loop_add is 219.The sum is 20000000.The time length is 31.The sum is 20000000.

重复调用lua脚本中的函数1千万次,大概2秒。

在脚本中循环,大概0.2秒。

直接在C语言中循环,大概0.03秒。