Sign in to follow this  
Drakath

outputChatBox

Recommended Posts

What is more efficient?

This:

for _,v in ipairs(getElementsByType("player")) do 
outputChatBox("Hello",v) 
end 

or this:

outputChatBox("Hello",root) 

Share this post


Link to post
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.

Share this post


Link to post

The element tree from wiki explains why ;)

Tre.png

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.

Share this post


Link to post

Even faster:

local players = getElementsByType("player") 
for i=1, #players do 
    outputChatBox("Hello", players[i]) 
end 

Share this post


Link to post

Btw use pairs for loop since ipairs is 80% slower than pairs.

Share this post


Link to post

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 

?

Share this post


Link to post

ipairs loop only number indexes "start from 1 index " and only in order but pairs loop all.

Share this post


Link to post

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?

Share this post


Link to post

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%)

Share this post


Link to post

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.

Share this post


Link to post

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.

Share this post


Link to post

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!

Share this post


Link to post
I'm not using pairs() or ipairs()...

it's just an advice so yes the last code is the most efficient way possible.

Share this post


Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.