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

本文通过一个C语言程序展示了调用lua脚本执行数学运算的效率。测试了lua脚本中的add和loop_add函数,以及C语言自身进行相同运算的时间。结果表明,直接在C语言中循环执行速度最快,lua脚本调用次之,脚本内部循环最慢。
摘要由CSDN通过智能技术生成

// 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);   /*

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值