为什么 Lua 中的本地化函数更快? [英] Why are localized functions faster in Lua?

查看:17
本文介绍了为什么 Lua 中的本地化函数更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

local min = math.min

结果:

非本地:0.719 (158%)
本地化:0.453 (100%)

结论:

是的,我们应该本地化所有标准的 lua 和 Spring API 函数.

来源:https://springrts.com/wiki/Lua_Performance

 

性能提升的原因是什么?

What is the reason for that performance boost?

推荐答案

局部最小值 = math.min

local min = math.min

请记住,table.name 只是 table["name"] 的语法糖(它们完全等效).全局变量只是环境表中的键,所以 math.min_ENV["math"]["min"].这是获得实际函数值的两次哈希表查找.

Remember that table.name is just syntax sugar for table["name"] (they're exactly equivalent). And globals are just keys in the environment table, so math.min is _ENV["math"]["min"]. That's two hashtable lookups to get at the actual function value.

将值复制到 local 会将其放入 VM 寄存器中,因此无需查找.

Copying the value into a local puts it in a VM register so there's no lookup.

这篇关于为什么 Lua 中的本地化函数更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆