Using i,v in pairs(), it sepratley does it on each object, is there anyway it can do it all at the same time.
Sorry if im not being clear.
Using i,v in pairs(), it sepratley does it on each object, is there anyway it can do it all at the same time.
Sorry if im not being clear.
Yes, the foreach loop does procedurally go through each item. If you need to do something with a bunch of items with no delay, you can use indexes
Could you give me an example of something like this?
Technically all scripts have delays going between lines, but it’s so small that you really won’t notice it!
If you are using a for i,v in pairs() loop, as long as you don’t have a wait() you should not have to worry about the delay. (Unless you are going through a insane amount of objects and editing all of them, like thousands upon thousands.) All the objects should be happening at pretty much the same time!
I did some quick benchmarking, so this post includes the results with short explanation.
Procedure: I took a standard path and measured 5 results for each function, and later removed the two most deviant ones. Speed is than compared using averages.
Task: I thought to myself that the most accurate way to compare raw speed is searching for a number in a large array. If we were to measure timing of, for instance, how fast we can loop through surface lights and change their color, that wouldn’t be as accurate, because other processes would be involved. The number array contained numbers ranging from 1 to 5 *10^6, and the functions were looking for the last number, which was 5 million in our case. That is the worst case we can get, as any lower number would be found faster.
First one was for-loop. It found the number in 0.06976 seconds, which is pretty quick. table.find() was slightly faster (around 2.9 times), with the average of 0.0241947 seconds. As you can see loops are extremely fast.
Imagine that 5 million unique players joined our game, 4 million of which would be bots (just kidding ), and we kept a record of all players who joined at least once. Dictionaries can check for particular player in a table under 0.2e-6 s, unlike tables, to which above results apply. The difference doesn’t play a key role when it comes to lower amounts of elements (for example under 10^5), but does with higher amounts, just like @kingerman88 said. Looping through huge tables requires a coroutine running to insert an occasional wait interval, albeit use of wait() is discouraged because of it’s superior alternative RunService.Heartbeat:Wait() - we are talking about reliability and consistency here.
As stated before me, pairs() and ipairs() is very fast, so you don’t need to act on all objects at the same time. Doing that would be an overcomplication. If you really tens of millions of objects, you could create coroutines, looping through sections of a table, e.g. one beginning at the start, one at 1/3, and one at 2/3. Again, that is an extremely rare case. How are both types of tables used?
Lua table library applies. It’s fast, privileged and pretty efficient. You can loop through arrays, use them combined with pairs() as well as ipairs(), and so on.
local dictionary = {}
for i = 1, 10, 1 do
dictionary[i] = 1 * 10
end
--[[
Data structure:
dictionary = {
[1] = 10;
[2] = 20;
...
} ]]
Indexes don’t have to be numbers:
local dictionary = {
number1 = 1;
number2 = 2;
-- Or indexes like this, if we
-- they contain special characters
-- or white spaces:
["White space"] = Enum.KeyCode.Space;
}
Dictionaries can contain dictionaries or array inside of them:
local dictionary = {
["Array container"] = {
{"number", 1};
{"number", 2};
};
-- or
["Dictionary container"] = {
["speed"] = 50;
["stamina"] = 60;
}
}
Let’s take a look at loops. We will be using the following dictionary in all examples.
local dictionary = {
["First element"] = 1;
["Second element"] = 2;
["Third element"] = 3;
}
for i, v in pairs(dictionary) do
print(i, v)
end
-- OUTPUT
Third element 3
Second element 2
First element 1
Why don’t we use ipairs() to ensure correct order? We cannot, but there are some trick to order a dictionary. I will not be stating them here, because Dev Forum has multiple posts addressing this obstacle.
for i = 1, #dictionary do
print(i, dictionary[i])
end
-- OUTPUT is empty, because we can't get the length of the dictionary
-- using # operator. for-loop seem like a solution.
How to check whether dictionary is empty or not? Same way as we check arrays.
local empty = next(dictionary) and true or false
-- Alternative for arrays:
local empty = #array > 0 and true or false
Hopefully, this post helps!