我有一个特殊用户和普通用户的列表。特殊用户有自己特殊的功能,而普通用户使用标准功能。lua中的内部函数性能
我想出了这个代码设计,但我觉得这不是最佳的(性能明智)。
所以我的问题是:如何在调用如下例所示的内部函数时获得最佳性能?
if something then
CallFunc(var)
end
特殊/正常用户逻辑
function CallFunc(var)
if table[name] then
table[name](var)
else
Standard_Func(var)
end
end
local table = {
["name1"] = function(var) Spec_Func1(var) end,
["name2"] = function(var) Spec_Func2(var) end,
["name3"] = function(var) Spec_Func3(var) end,
...
--40 more different names and different funcs
}
特殊用户funcs中
function Spec_Func1(var)
--lots of code
end
function Spec_Func2(var)
--lots of code
end
...
--more funcs
编辑: 看到@ hjpotter92的回答是:
我不能在表中查找用户。
local function_lookups = {
name1 = Spec_Func1, --this doesnt let me find the user
--name1 = 1 --this does let me find the user (test)
}
if function_lookups[name] then --this fails to find the user
--do something
end
大概'Spec_Func1'是在查找表之后定义的? – hjpotter92
facepalm .......... –
特殊用户是否都提到相同的功能?普通用户是否都被称为相同的功能?这些功能是否在同一行动中被调用?如果这些条件成立,你的工作变得更容易。 – warspyking