Re: More ipairs Confusion
Posted: Sat Aug 06, 2016 8:53 am
I think I'd like to comment Zorg's reply.
Please note, that I'm not into arguing as there's nothing wrong with what I'd like to comment
Arbitrary order one gets from pairs may not be acceptable to get the sequence of lines of text, true.
But when you have to delete things from a table while still traversing it - the pairs lets you write an easier implementation.
Look at this sample with pairs:
Now, ipars won't go on you that easy. The example below will skip entries in some_table. Every time you delete something, the NEXT field will be skipped. Say, you've deleted some_table[1]. It means that some_table[2] will be skipped.
There's a lot of talks on the matter. The best solution I've found would probably be this one: http://stackoverflow.com/a/12397742
Now I'll move on to execution time (i.e. the speed).
The pairs iterator isn't always slower that ipairs.
Some guy at Garry'sMod forums have posted this: https://facepunch.com/showthread.php?t=875909
Also here are some benchmarks posted @lua-users: http://lua-users.org/lists/lua-l/2009-11/msg01007.html
Here's my own measurements too:
Actual code:
The results:
Now, surprisingly enough, If I edit the source like this:
^ Note the one-liner
The results would be different:
So... For love2d built with LuaJIT pairs *is* actually slower than ipairs.
But I'd say it all depends on what Lua implementation one is working with, how it is integrated into a framework and what coding-style habbits one exerts.
The numbers can't lie
Please note, that I'm not into arguing as there's nothing wrong with what I'd like to comment
Arbitrary order one gets from pairs may not be acceptable to get the sequence of lines of text, true.
But when you have to delete things from a table while still traversing it - the pairs lets you write an easier implementation.
Look at this sample with pairs:
Code: Select all
for k,v in pairs(some_table) do
if v.dead then
some_table[k] = nil
end
end
Code: Select all
for k,v in ipairs(some_table) do
if v.dead then
table.remove(some_table, k)
end
end
Now I'll move on to execution time (i.e. the speed).
The pairs iterator isn't always slower that ipairs.
Some guy at Garry'sMod forums have posted this: https://facepunch.com/showthread.php?t=875909
Also here are some benchmarks posted @lua-users: http://lua-users.org/lists/lua-l/2009-11/msg01007.html
Here's my own measurements too:
Actual code:
Code: Select all
local t = {}
for i=1,10000 do
t[i] = math.random(1,1000000)
end
local time = os.clock()
for k,v in pairs(t) do
end
print(os.clock()-time)
local time = os.clock()
for k,v in ipairs(t) do
end
print(os.clock()-time)
Code: Select all
Lua 5.2
pairs: 0.000897
ipairs: 0.000666
LuaJIT
pairs: 0.000117
ipairs: 0.000068
Love2d with LuaJIT:
pairs: 0.000045
ipairs: 0.000035
Code: Select all
local t = {}
for i=1,10000 do t[i] = math.random(1,1000000) end
local time = os.clock()
for k,v in pairs(t) do
end
print(os.clock()-time)
local time = os.clock()
for k,v in ipairs(t) do
end
print(os.clock()-time)
The results would be different:
Code: Select all
Lua 5.2
pairs: 0.001395
ipairs: 0.001443
LuaJIT
pairs: 0.00016
ipairs: 0.000094
Love2d with LuaJIT
pairs: 0.000044
ipairs: 0.000028
But I'd say it all depends on what Lua implementation one is working with, how it is integrated into a framework and what coding-style habbits one exerts.
The numbers can't lie