21

I have read that the use of ipairs is slow compared to a for loop, should I change my programming habit? I'll be using lua 5.2 one day, currently 5.1.

My arrays are approximately 1000 items at most.

local mytbl = { 'a','b','c','e'}
for i,v in ipairs(mytbl) do
  print(i,v)
end

for i=1,#mytbl do
  print(i,mytbl[i])
end
topskip
  • 16,207
  • 15
  • 67
  • 99

1 Answers1

32

http://springrts.com/wiki/Lua_Performance#TEST_9:_for-loops

pairs: 3.078 (217%)
ipairs: 3.344 (236%)
for i=1,x do: 1.422 (100%)
for i=1,#atable do 1.422 (100%)
for i=1,atable_length do: 1.562 (110%)

Note, however, that using a numerical for loop only works if you're iterating over tables with sequential numeric indices - if you're using hash keys for your tables, or sparse tables, then you'll need to use some form of pairs().

danShumway
  • 450
  • 4
  • 15
Amber
  • 507,862
  • 82
  • 626
  • 550
  • Thanks for the performance table. A double time is actually not much when it comes to just a few loops, so I am not in a big need to change my current code. But for the future loops, I know now what I need to do. Thanks! – topskip Jan 21 '12 at 17:56
  • 6
    As with all performance considerations, it all depends on context. Premature optimization being the root of all evil and whatnot. – Amber Jan 21 '12 at 18:02
  • Your comment should read: **"then you'll need to use `pairs()`"** There are only two "forms of pairs": `pairs` and `ipairs`. The latter is exactly equivalent to a for loop: it iterates by incrementing a numerical index from `1` to `#t`. It won't work in your example cases for the same reason the `for` loop won't. – Mud Jul 28 '14 at 20:56
  • @Mud Actually `ipairs(t)` and a numeric for loop from `1` to `#t` are _not_ equivalent. The former iterates over numeric keys and values until a `nil` value is found. The latter iterates over all numeric keys from `1` to the length of the table as it was at the beginning of the iteration. If `t` does not change during iteration and `t` does not have holes, _then_ the two loops are equivalent (except when the `__len` and `__ipairs` metamethods are involved). – siffiejoe Jul 29 '14 at 21:23
  • I misspoke in calling them *exactly* equivalent. However, they're equivalent in terms of "only works if you're iterating over tables with sequential numeric indices", which is what I was talking about. – Mud Jul 29 '14 at 22:00
  • If you want to be pedantic, one could easily make the argument that `ipairs()` is not a form of `pairs()` given that it is not strictly a superset of `pairs()` functionality (it provides features that `pairs()` does not, namely ordering). In this case "some form of `pairs()` was merely another wording of "some incantation involving `pairs()`". – Amber Jul 30 '14 at 02:33
  • I don't understand what they mean by `i=1,atable_length`. How is that any different than `for i=1,x`, besides the variable name? – 12Me21 Jun 26 '18 at 21:53
  • @12Me21 the 5 times listed there directly correspond to the 5 code examples cited prior in the chapter. – Amber Jun 28 '18 at 01:59