为什么在Lua中本地化功能更快? [英] Why are localized functions faster in Lua?

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

问题描述

测试1:本地化

代码:

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?

推荐答案

本地分钟=数学分钟

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天全站免登陆