Why are localized functions faster in Lua?

大兔子大兔子 提交于 2021-02-04 15:56:46

问题


TEST 1: Localize

Code:

local min = math.min

Results:

Non-local: 0.719 (158%)
Localized: 0.453 (100%)

Conclusion:

Yes, we should localize all standard lua and Spring API functions.

Source: https://springrts.com/wiki/Lua_Performance

 

What is the reason for that performance boost?


回答1:


local min = 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.

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



来源:https://stackoverflow.com/questions/32945039/why-are-localized-functions-faster-in-lua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!