为什么在lua中局部变量的访问速度比全局变量快? [英] Why are local variables accessed faster than global variables in lua?
问题描述
所以我在读Lua 2nd Ed的程序设计,我在这里看到了这一段:
它只要
可能,就使用局部变量是很好的编程风格。局部变量可帮助您避免使用不必要的名称混淆全局
环境。此外,本地
变量的访问速度快于全球变量。
任何人都可以解释这是为什么?这个功能仅在Lua中,还是在其他语言中? (例如C,C ++,Java)
运行时间的差异是由于哈希表查找和数组查找之间的差异。解释器可能能够将局部变量放置在CPU寄存器中,但即使没有这种聪明性,局部变量也可以更快地访问。
Lua中的全局变量存储在表中。通常,任何人都可以修改这些表,因此解释器每次访问时都必须重新查找一个值。另一方面,局部变量只有在超出范围时才会消失。因此它们可以在数组中有固定的位置。
下面的基准程序在循环中调用一个虚拟函数。基准测试显示了程序运行时间越多,程序必须跳过的表越多。
其他动态语言应该具有相似的特性;
一些相关链接:
- 使用本地变量进行优化(Lua)
- < a href =http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Local_Variables =nofollow>本地变量(Python性能提示)
- 优化全局变量/属性访问。关于查找的(撤回)Python提案全局与本地对象。
文件 demo.lua
: p>
local M = {}
_G.demo = M
函数M.op(x)return x end
return M
文件 main.lua $ c $
本地M =需要演示
op = demo.op
本地outer_op = demo.op
函数iter_op(n)
本地inner_op = demo.op
for i = 1,n do
- n = 100,000,000(Lua 5.2.0)的运行时间示例:
- 查找表格_G),然后在该表中查找'op'
-
-
- demo.op(i) - > 0:40
- _G.op(i) - > 0:39
- 在已知本地表(M或
- 全局表)中查找'op':
-
- M .op(i) - > 0:30
- op(i) - > 0:30
$ b $ - 解引用在
内部或外部声明的局部变量 - this iter_op()函数:
-
- inner_op(i) - > 0:23
- outer_op(i) - > 0:22
结束
结束
iter_op(100000000)
文件 main.py
:
import demo#包含'def op(x):return x'。
global_op = demo.op
def iter_op(n):
local_op = demo.op
for xrange(n):
#n = 50,000,000(Python 2.6.5)的运行时间示例:
#demo.op(i)#0:50
#global_op(i)#0:41
local_op (i)#0:36
iter_op(50000000)
So I was reading Programing in Lua 2nd Ed and I came across this paragraph here:
It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.
Could anyone explain why this is? And is this "feature" only in Lua, or is it in other languages aswell? (e.g. C, C++, Java)
The difference in running time is due to the difference between hash table lookup and array lookup. An interpreter might be able to place a local variable in a CPU register, but even without such cleverness local variables are faster to access.
Global variables in Lua are stored in tables. Generally, anyone can modify these tables, and therefore the interpreter has to lookup a value anew every time it is being accessed. Local variables on the other hand disappear only when they go out of scope. Therefore they can have fixed locations in an array.
The benchmark program below calls a dummy function in a loop. The benchmark shows how the running time goes up the more tables the program has to jump through.
Other dynamic languages should be expected to have similar characteristics; see for example the Python benchmark at the very end.
Some relevant links:
- Optimising Using Local Variables (Lua)
- Local Variables (Python performance tips)
- Optimizing Global Variable/Attribute Access. A (withdrawn) Python proposal on the lookup of global vs. local objects.
File demo.lua
:
local M = {}
_G.demo = M
function M.op(x) return x end
return M
File main.lua
:
local M = require "demo"
op = demo.op
local outer_op = demo.op
function iter_op(n)
local inner_op = demo.op
for i = 1, n do
-- Example running times for n = 100,000,000 (Lua 5.2.0):
-- Lookup a table (demo or _G), then lookup 'op'
-- within that table:
--
-- demo.op(i) --> 0:40
-- _G.op(i) --> 0:39
-- Lookup 'op' within a known local table (M or the table of
-- globals):
--
-- M.op(i) --> 0:30
-- op(i) --> 0:30
-- Dereference a local variable declared inside or outside
-- of this iter_op() function:
--
-- inner_op(i) --> 0:23
-- outer_op(i) --> 0:22
end
end
iter_op(100000000)
File main.py
:
import demo # Contains 'def op(x): return x'.
global_op = demo.op
def iter_op(n):
local_op = demo.op
for i in xrange(n):
# Example running times for n = 50,000,000 (Python 2.6.5):
# demo.op(i) # 0:50
# global_op(i) # 0:41
local_op(i) # 0:36
iter_op(50000000)
这篇关于为什么在lua中局部变量的访问速度比全局变量快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!