Drakath Posted November 7, 2014 Posted November 7, 2014 What is more efficient? This: for _,v in ipairs(getElementsByType("player")) do outputChatBox("Hello",v) end or this: outputChatBox("Hello",root)
Grafu Posted November 7, 2014 Posted November 7, 2014 What is more efficient?This: for _,v in ipairs(getElementsByType("player")) do outputChatBox("Hello",v) end or this: outputChatBox("Hello",root) This one, I guess: for _,v in ipairs(getElementsByType("player")) do outputChatBox("Hello",v) end Another one is 30% slower than this one.
Mr_Moose Posted November 7, 2014 Posted November 7, 2014 The element tree from wiki explains why The chat message are only shown to players but with root as element it has to go through all elements, even those who's not goanna view the chat.
MTA Team botder Posted November 7, 2014 MTA Team Posted November 7, 2014 Even faster: local players = getElementsByType("player") for i=1, #players do outputChatBox("Hello", players[i]) end
Saml1er Posted November 7, 2014 Posted November 7, 2014 Btw use pairs for loop since ipairs is 80% slower than pairs.
Drakath Posted November 7, 2014 Author Posted November 7, 2014 So when should I use pairs and when ipairs? Or should I always use pairs instead? Is it more efficient to use Necktrox's code or: for _,v in pairs(getElementsByType("player")) do outputChatBox("Hello",v) end ?
SkatCh Posted November 7, 2014 Posted November 7, 2014 ipairs loop only number indexes "start from 1 index " and only in order but pairs loop all.
Drakath Posted November 7, 2014 Author Posted November 7, 2014 I know what each does but what I asked was: when should I use ipairs and when pairs? And what about the code's efficiency?
MTA Team botder Posted November 7, 2014 MTA Team Posted November 7, 2014 See here: Lua Performance Test Results: 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%)
SkatCh Posted November 7, 2014 Posted November 7, 2014 as i told you ipairs is for indexed tables, it means every item <<<<>>>> in the table has it's own number: 1 is the index of the first value 2 is the index ot the second's and so on: local table1 = { 1, 2, 3, 4 } -- First type local table2 = { [1] = 1, [2] = 2, [3] = 3,[4] = 4} -- Second type for i, v in ipairs( table1 ) do --both pairs and ipairs will work in those tables: for i, v in pairs( table1 ) do but if you have a table like this local table = { ["Yes"] = 1, [1] = "Table", ["example"] = 2014, [2] = "me", } -- ipairs won't work in this case. So try to use pairs all the time.
Drakath Posted November 7, 2014 Author Posted November 7, 2014 Alright, thanks everyone. So as I understood, local players = getElementsByType("player") for i=1, #players do outputChatBox("Hello", players[i]) end is the most efficient way possible.
SkatCh Posted November 7, 2014 Posted November 7, 2014 you must read the conclusion : Conclusion: Don't use pairs() or ipairs() in critical code! Try to save the table-size somewhere and use for i=1,x do!
SkatCh Posted November 7, 2014 Posted November 7, 2014 I'm not using pairs() or ipairs()... it's just an advice so yes the last code is the most efficient way possible.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now