我有一個特殊用戶和普通用戶的列表。特殊用戶有自己特殊的功能,而普通用戶使用標準功能。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