Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 使用Lua表的my函数的效率_Performance_Lua_Lua Table - Fatal编程技术网

Performance 使用Lua表的my函数的效率

Performance 使用Lua表的my函数的效率,performance,lua,lua-table,Performance,Lua,Lua Table,我有一个关于我如何组合这段Lua代码的问题。 比如,有一个类似于下面的函数,包含200个myTable表,其中名称按字母顺序排列: function loadTable(x) local myTable if x == "aaron" then myTable = {1,2,3,4,5,6,7,8,9,0} elseif x == "bobby" then myTable = {1,3,3,4,5,8,7,8,9,1} elseif x == "cory" t

我有一个关于我如何组合这段Lua代码的问题。 比如,有一个类似于下面的函数,包含200个
myTable
表,其中名称按字母顺序排列:

function loadTable(x)
    local myTable
    if x == "aaron" then myTable = {1,2,3,4,5,6,7,8,9,0}
    elseif x == "bobby" then myTable = {1,3,3,4,5,8,7,8,9,1}  
    elseif x == "cory" then myTable = {1,2,3,3,3,6,7,8,9,2}
    elseif x == "devin" then myTable = {1,2,3,4,5,2,3,4,9,0}          
    ...
    else 
        print("table not available") 
    end
    return myTable
end
现在我想找到对应于
x==“zac”
(恰好在末尾的某个地方)的表。我使用这行代码:

local foundTable = loadTable("zac")

这不是一点效率都没有吗?如果必须在函数的最后找到表,则必须遍历前面的所有代码行。是否有某种方法可以在lua中更有效地编写此代码并更快地找到正确的表

使用。。。一张桌子

只需创建一个表,其键为人名,值为要加载的表,如下所示:

local tables = {
   john = {1,2,3,4,5,6,7,8,9,0},
   peter = {1,3,3,4,5,8,7,8,9,1},
   william = {1,2,3,3,3,6,7,8,9,2},
   victoria = {1,2,3,4,5,2,3,4,9,0}
   --...
}

然后,不要调用
loadTable(“richard”)
只需使用
tables[“richard”]
tables即可。richard
如果键是有效的,我不敢提这一点,因为他在填充表时可能会使用无效的标识符。但是,是的,在这个只使用名称的场景中,这会起作用,谢谢添加:)