performance - Lua table access efficiency -
I have a question about accessing data in a Lua table. Like, the following is a large Lua table:
tbl = {{blockIdx = 5, key1 = "val1", key2 = "val2", ...}, { BlockIdx = 30, key1 = "val11", key2 = "val12", ...}, {blockIdx = 83, key1 = "val21", key2 = "val22", ...}, ...} and now I have to find one of the blocks blockIdx , for example, 38 . So normally, I want to use the to find the block : , add in V (TBL) if v.blockIdx = = 38 then Black Function (v) End End But I don`t think this is a good idea especially for big tables. So I modify the table a bit:
tbl = {[5] = {key1 = "val1", key2 = "val2", ...}, [30] = {key1 = "Val11", key2 = "val12", ...}, [83] = {key1 = "val21", key2 = "val22", ...}, ...} Then I can easily use my block with a line:
blahfunction (tbl [38]) Then my The question is, is there a different demonstration between two methods?
Maybe tbl [38] What exactly was there for loop inside Leah? Or, like an array in C / C ++, we can access memory using [] without loop, witch, obviously better performance.
The display is different, the second method is more efficient.
Internally, an array part is divided into a lava table and a hash, if the table is a sequence, then the sequence portion is implemented by the array part. But in your second example there is no table sequence, it is possibly implemented by the hash part. In this case, performance is not like reaching arrays in C / C ++, but it is like reaching a hash, which is still faster.
Then in the conclusion, the second piece of code is fast, because it is' repeat through elements like your first example.
Comments
Post a Comment