More ipairs Confusion

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: More ipairs Confusion

Post by 4aiman »

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:

Code: Select all

for k,v in pairs(some_table) do
   if v.dead then
      some_table[k] = nil
   end
end
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.

Code: Select all

for k,v in ipairs(some_table) do
   if v.dead then
      table.remove(some_table, k)
   end
end
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:

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:

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
Now, surprisingly enough, If I edit the source like this:

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)
^ Note the one-liner

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
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 :)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: More ipairs Confusion

Post by zorg »

The results aren't that different though, the magnitude didn't change. I'm guessing you just didn't iterate over enough indices.

Also, just to make sure, since LuaJIT may be smart enough to optimize out the whole i/pairs loops since you're effectively doing nops in them...
Have you benchmarked with something inside them? That may skew the results a bit, just a hunch though. :3
Something like:

Code: Select all

local t = {}
for i=1,1000000 do t[i] = math.random(1,1000000) end -- also use a million iters, more iterations will smooth out any unwanted hiccoughs.

local time = os.clock()
for k,v in pairs(t) do
-- something
v = k+1
end
print(os.clock()-time)

local time = os.clock()
for k,v in ipairs(t) do
-- here too
v = k + 1
end
print(os.clock()-time)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: More ipairs Confusion

Post by 4aiman »

Here :)
Also, I've added a "for" loop just to get more results :crazy:

Code: Select all

local t = {}
for i=1,1000000 do t[i] = math.random(1,1000000) end -- also use a million iters, more iterations will smooth out any unwanted hiccoughs.

local time = os.clock()
for k,v in pairs(t) do
-- something
v = k+1
end
print(os.clock()-time)

local time = os.clock()
for k,v in ipairs(t) do
-- here too
v = k + 1
end
print(os.clock()-time)

local time = os.clock()
local v = 0
for i=1, #t do
-- here too
    t[i] = i + 1
end
print(os.clock()-time)

Code: Select all

Lua 5.2
pairs:  0.041717
ipairs: 0.048073
for:    0.023626

LuaJIT
pairs:  0.012647
ipairs: 0.002262
for:    0.001787

Love with LuaJIT
pairs:  0.005607
ipairs: 0.001106
for:    0.001494
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests