for _, id in pairs(player(0, 'table')) do
VS
for id = 1, 32 do if player(id, 'exists') then
I personally think the second one is faster, the first one is slower.
edited 2×, last 03.06.20 01:28:40 pm
for _, id in pairs(player(0, 'table')) do
for id = 1, 32 do if player(id, 'exists') then
function test() 	startTime = os.clock() 	 	for i = 1, 1000 do 		for _, id in pairs(player(0, 'table')) do 		end 	end 	print(os.clock() - startTime) 	startTime = os.clock() 	 	for i = 1, 1000 do 		for id = 1, 32 do 			if player(id, 'exists') then 			end 		end 	end 	print(os.clock() - startTime) end
local players = player(0, 'table') for i = 1, #players do local p = players[i] -- player id is stored in the p variable end
#player(0, 'table')is equal to 3 instead of 5 or for each 1,3,5 in this case. I hope you understand my poor explanation. Correct me If I'm wrong, please.
#player(0, 'table')is equal to 3 instead of 5 or for each 1,3,5 in this case. I hope you understand my poor explanation. Correct me If I'm wrong, please.
pairs()and
ipairs()are slower.
local players = player(0, 'table') for index = 1, #players do local player_id = players[index] -- or just call it "id" var end
#playersin another variable too, like:
players_n, but that won't speed up the code so much.
for i= 1, #player(0, 'table') do local id = players[i] end for _, id in pairs(player(0, 'table')) do end
function test() 	startTime = os.clock() 	 	for i = 1, 10000 do 		for _, id in pairs(player(0, 'table')) do 			 		end 	end 	print(os.clock() - startTime) 	 	 	startTime = os.clock() 	for i = 1, 10000 do 		for id = 1, 32 do 			if player(id, 'exists') then 			 			end 		end 	end 	print(os.clock() - startTime) 	 	 	startTime = os.clock() 	 	for i = 1, 10000 do 		local players = player(0, 'table') 		 		for i = 1, #players do 			local p = players[i] 			 			-- player id is stored in the p variable 		end 	end 	print(os.clock() - startTime) end
player(0, 'table') vs 32 calls to that method.
for #playersand
for pairs(players).
image()and so on where you're about to work on big data like 400 mobs on a map. Therefore you need to cache some data somehow and divide the map into chunks for example.
playerinstead by adding
local player = _G.playerat top of your scripts. That helps in performance slightly because local variables accessed faster than global variable. Of course you're not limited to
playerfunction. You can do it to any functions.