OC直接调用Lua与LuaScriptCore对比

网上有大佬用OC封装了一下Lua,方便c语言不纯属的iOS开发者调用。他是这么说的:而且Lua的C Api相对来说不容易理解和掌握,需要投入一定的时间去学习和累积这方面的经验。而**[LuaScriptCore](https://github.com/vimfung/LuaScriptCore)**就是为了解决这些问题而诞生,他简化了扩展Lua的难度,让开发者可以通过自己熟悉的语言来扩展Lua,完美解决Lua与原生代码之间的通讯问题。
想参考学习LuaScriptCore的同学可以跳:https://blog.csdn.net/vimfung/article/details/53608107
下面开始我的表演 ???
首先创建一个config.lua文件,里面简单写句代码,我是些了一个颜色,一个两数相加的方法

config.lua里面的代码

接下来,导入头文件

#import "LuaScriptCore.h"
@property (nonatomic, strong) LSCContext *context;

我们现在viewDidLoad里面实现初始化和加方法的代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"1");

    self.context = [[LSCContext alloc] init];
    [self.context evalScriptFromFile:@"config.lua"];
    NSLog(@"2");
    
    LSCValue *funcValue = [self.context callMethodWithName:@"addFunc" arguments:@[[LSCValue numberValue:@(1000)], [LSCValue numberValue:@(24)]]];
    NSLog(@"3");
    
    NSLog(@"funcValue = %ld", [funcValue toInteger]);
    NSLog(@"end");
    
}

运行,看结果:

运行结果

不难看出, LSCContext初始化并读取lua文件用时1.3s
接下来加入改变VC.view颜色的代码继续运行

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"1");

    self.context = [[LSCContext alloc] init];
    [self.context evalScriptFromFile:@"config.lua"];
    NSLog(@"2");
    
    LSCValue *funcValue = [self.context callMethodWithName:@"addFunc" arguments:@[[LSCValue numberValue:@(1000)], [LSCValue numberValue:@(24)]]];
    NSLog(@"3");
    
    NSLog(@"funcValue = %ld", [funcValue toInteger]);
    NSLog(@"end");
    
    
    LSCValue *value = [self.context evalScriptFromString:@"return backgroundColor"];
    
    if (value.valueType == LSCValueTypeNumber)
    {
        UInt32 color = (UInt32)[value toInteger];
        self.view.backgroundColor = [self colorWithRGBHex:color];
    }
}
- (UIColor *)colorWithRGBHex:(UInt32)hex {
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex) & 0xFF;
    return [UIColor colorWithRed:r / 255.0f
                           green:g / 255.0f
                            blue:b / 255.0f
                           alpha:1.0f];
}

运行之后看结果:

运行结果

依旧是1.3s,然而,对于开发的我们而言,1.3秒不是小数字,每次读取lua文件都要花费1.3s的话,那无疑会大幅度的降低用户体验。接下来我们放弃 LuaScriptCore使用lua原生库来继续测试。
首先导入

#import "lua.h"
#import "lauxlib.h"
#import "lualib.h"
@property (nonatomic) lua_State *state;

接下来

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"1");
    self.state = luaL_newstate();    //创建新的lua_State结构体
    luaL_openlibs(self.state);        //加载标准库

    NSLog(@"2");

    NSString *fn = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"lua"];

    NSLog(@"3");

    if (luaL_dofile(self.state, [fn UTF8String])) {
        NSLog(@"error: %s", lua_tostring(self.state, -1));
    }
    NSLog(@"4");

    lua_getglobal(self.state, "backgroundColor");
    UInt32 value = lua_tonumber(self.state, -1);
    NSLog(@"backgroundColor = %u", (unsigned int)value);
    lua_pop(self.state, 1);

    UInt32 color = value;
    self.view.backgroundColor = [self colorWithRGBHex:color];
    
    NSLog(@"5");
    
    lua_getglobal(self.state, "addFunc");
    lua_pushinteger(self.state, 1000);
    lua_pushinteger(self.state, 24);

    /*
     1. 调用的
     2. 参数数量
     3. 返回值的数量
     4. 错误码
     **/
    lua_pcall(self.state, 2, 1, 0);

    NSInteger retVal = lua_tonumber(self.state, -1);
    NSLog(@"retVal = %ld", retVal);
    
    NSLog(@"end");
}

然后运行看结果

运行结果

不难看出, lua_State加载库用时0.003s,读取lua用时0.0002s,操作lua文件用时0.0016s 基本忽略不计,同时也能看出不管是获取方法计算,还是获取值都没有耗时。

结论

直接调用c Api的Lua没有耗时,而调用大佬的LuaScriptCore时,初始化读取Lua文件会耗时0.8s - 1.6s (多次测试得出的结论)。
接下来就进入LuaScriptCore调用文件的方法去看一看到底是什么原因造成的耗时:
进入[self.context evalScriptFromFile:@"config.lua"];方法来一看究竟。
LuaScriptCore在初始化的时候,调用了

//初始化类型导出器
        self.exportsTypeManager = [[LSCExportsTypeManager alloc] initWithContext:self];

点进去看,执行了

//初始化导出类型
        [self _setupExportsTypes];
for循环

在查找所需类型时,使用了for循环,而循环的次数是11888次,那么造成的耗时是正常的。所以,如果想用OC调用Lua的同学可以自行修改并使用,也可以直接调用原生c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值