I mean, I've looked at the wiki page... But I simply don't see why'd you use this over "pairs"... I understand for ordering or whatever, but why can't you just order it with a little extra work and use pairs instead? Or is this just literally impossible?
I hate learning to code. This shit is hard
There are next to no performance gain to use pairs over ipairs Speed Analysis of pairs and ipairs.
The difference is that ipairs runs through the table until it finds a nil, where pairs keeps going.
t = {"Hello", nil, "how", "are", "you"}
for _,str in ipairs(t) do print(str) end -- Will only print "Hello"
for _,str in pairs(t) do print(str) end -- Will print "Hello how are you"
I see, thanks mate!
It's still a lot better to use ipairs if you can.
It's probably good form, but I honestly don't really care about a 1-ish% speed increase in a loop (opposed to pairs or next) unless I have to constantly loop through something with a fuckton (tens of thousands) of items, and I've never come across a situation where that was necessary. I can't think of a situation where that would ever need to happen repeatedly in Garry's Mod either.
The only time I ever use an interator as opposed to a forloop is if the table in question has info on the hash side, and that'd be pairs/next. I guess I like the aesthetic of a for loop more than ipairs?
I just can't see why ipairs is specifically better than a normal for loop for a non-sparse table.
This is exactly what I was thinking.
Worth noting: with pairs there is no guarantee that the order will go 1, 2, 3, etc. In some cases it can be shuffled around. With ipairs you have a guarantee that you start at 1 and increase by 1 each time.
I kinda remember that people tested this in gmod and found out that ipairs is faster than a general for loop thanks to LuaJIT. Performance isn't the only thing you should worry about, but it's something to keep in mind.
Just tested this and you are correct, performance-wise.
So basically use ipairs when you need increments of 1? I'm guessing this is pertaining to the actual keys of the table.
I was wondering why they used ipairs in the inventory example on the wiki. I guess I know now.
That's exactly what we were talking about, and no, ipairs is faster.
Didn't mean to be rude, just wanted to clarify
Sorry, you need to Log In to post a reply to this thread.