Lua 4 最外层变量作用域
Lua 4 outermost variable scope
我是否应该在 a 的最外层范围内显式声明一个变量 Lua 4 脚本 a
局部变量还是全局变量?我应该避免其中一个吗?谢谢
例如:
local foo = 5
对比
faa = 8
相信你已经知道local
变量是只存在于一定范围内的变量,而普通变量是global
,属于_G
[=21] =].然而,根据 Lua's Performance Guide,它也有助于使您的代码更快:
Lua precompiler is able to store all local variables in registers. The result is that access to local variables is very fast in Lua. For instance, if a
and b
are local variables, a Lua statement like a = a + b
generates one single instruction.
So, it is easy to justify one of the most important rules to improve the performance of Lua programs: use locals!
我是否应该在 a 的最外层范围内显式声明一个变量 Lua 4 脚本 a 局部变量还是全局变量?我应该避免其中一个吗?谢谢
例如:
local foo = 5
对比
faa = 8
相信你已经知道local
变量是只存在于一定范围内的变量,而普通变量是global
,属于_G
[=21] =].然而,根据 Lua's Performance Guide,它也有助于使您的代码更快:
Lua precompiler is able to store all local variables in registers. The result is that access to local variables is very fast in Lua. For instance, if
a
andb
are local variables, a Lua statement likea = a + b
generates one single instruction.
So, it is easy to justify one of the most important rules to improve the performance of Lua programs: use locals!