[XLua Pit] The difference between using pairs and ipairs

concept:

1. Table is a sequential table, ie {[1] = 1, [2] = 2, ...} or {1, 2, ...} 

2. When the table is a hash table, that is {["a"] = 1, ["b"] = 2, ...} or {a = 1, b = 2}

pairs can traverse the sequence table and hash table, generally only traverse the hash table

Ipairs can only traverse the sequence table. Generally, the sequence table only uses ipairs to traverse without pairs, because pairs consume more than iparis.

The problem often lies in

①When I am used to using ipairs, various abnormal problems occur when traversing the hash table, because iparis cannot traverse the hash table correctly.

②When you are accustomed to using pairs, there will be no obvious error reporting problems, but the performance may be reduced, if you use pairs to traverse all the sequence tables.

[Updated at 14:43:37 on January 30, 2021]

Pairs consumes the same as ipairs when traversing the sequence table. The consumption of pairs is larger than ipairs when traversing the hash table, because ipairs only traverse the sequence part and exit when it encounters nil.

Inside the pairs source code, the same code of ipairs is executed first, and then the hash part data traversal is executed. If there is no data, it will not be executed. Therefore, the consumption of ipairs and pairs is the same when traversing the sequence table!

Guess you like

Origin blog.csdn.net/qq_39574690/article/details/113418455