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 11:28:40 am
Scripts
Which one is faster to be executed?
Which one is faster to be executed?
1

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
SQ: Thank you
Mami Tomoe: This one won't work, Thanks anyway ;p
The Dark Shadow: what do you mean I use it quite often.
Mami Tomoe: Yeah, it's totally incorrect code, that should never work by design. You should reconsider your coding path. #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.
The Dark Shadow has written#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



Gaios: You are so popular
Mami Tomoe: Oh yeah, you are legendary
Mami Tomoe's approach to improve it doesn't give an even bigger performance boost.player(0, 'table') vs 32 calls to that method.
Mami Tomoe: In your calculations you got microtime, and the actual results for your tests for 32 players are extra 28ms (for pairs() loop) and 19ms (for #players loop) server delay. So the server will freeze for those miliseconds
. But let's assume people generally won't loop 1000 times.
There's no real difference in CS2D Lua scripts bettwen for #playersand
for pairs(players).
It doesn't make any difference on small data like 32 players in ms100 loop, and fetching like 4 properties (eg. health, x, y, rot) in each loop. I got like extra 1-3ms server delay each 0.1s.
CS2D Lua performance will only matter if you're about to script custom Mob engine, using eg. 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.
You should highly consider to optimize your CS2D Lua scripts when you're about to work on big data or you gonna use "always" hook, imao you should always avoid using "always" hook.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.
1
