On 2010-12-18 <12:35:38>, Hans Hagen wrote:
On 18-12-2010 12:03, Philipp Gesang wrote:
Depending on whether you want to access the non-hashed content as well you might want to use the “next” iterator instead as it’s slightly faster (according to my tests, that is). You won’t get around the local variable, though; I have no clue and no time to check if it’s even technically feasible to iterate a hash table without accessing the hashes.
indeed, for k, v in next, sometable do ... is faster as it saves one function call (i.e. pairs returning the next, table)
for indexed iteration using for i=1,n do ... is much faster than ipairs as it involves no function calls
Well, according to a quick test using “next” with hash tables has only a *very* slight advantage (in time) over pairs. With arrays the difference to ipairs() is much higher; in mixed arrays/hashes it might turn out even higher. For tables I came up with this relation (“<” meaning less execution time than): while < for < repeat < next < ipairs where “repeat … until” and “while … do … end” check if the final index has been reached. But the difference between the three loops is rather neglegible. What always baffled me is that in the manual Roberto advertises ipairs() as the iterator of choice (at least in the v.5.0 doc). Compared to the other options using it is just, well, erratic. Regard, Philipp -- () ascii ribbon campaign - against html e-mail /\ www.asciiribbon.org - against proprietary attachments