Forum

> > CS2D > Scripts > Which one is faster to be executed?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Which one is faster to be executed?

15 replies
To the start Previous 1 Next To the start

old Which one is faster to be executed?

The Dark Shadow
User Off Offline

Quote
As you can see the title above indicated my question.

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

old Re: Which one is faster to be executed?

SQ
Moderator Off Offline

Quote
Apparently you are wrong. table pairs in this case are 10X faster.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

This returns 0.001 for pairs and 0.01 for regular loop.
Difference depends on how many players are in the game.

old Re: Which one is faster to be executed?

Mami Tomoe
User Off Offline

Quote
That is because in the first example you're contacting CS2D only once while in the second it's up to 32 times.

This should be a good combination of the two:

1
2
3
4
5
6
7
local players = player(0, 'table')

for i = 1, #players do
     local p = players[i]
     
     -- player id is stored in the p variable
end

old Re: Which one is faster to be executed?

The Dark Shadow
User Off Offline

Quote
Okay, let me explain. Let's assume we have 3 players with ID: 1,3,5. So according to your code
#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.
edited 1×, last 04.06.20 04:15:47 pm

old Re: Which one is faster to be executed?

Gaios
Reviewer Off Offline

Quote
user The Dark Shadow has written
Okay, let me explain. Let's assume we have 3 players with ID: 1,3,5. So according to your code
#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.

Yes.

This code is the fastest that you can even write in Lua, using CS2D API... it's called variable buffer. And yes..
pairs()
and
ipairs()
are slower.
1
2
3
4
local players = player(0, 'table')
for index = 1, #players do
     local player_id = players[index] -- or just call it "id" var
end
...unless you would cache all player's IDs to Lua table (this would be the fastest), and yeah.. you would also store
#players
in another variable too, like:
players_n
, but that won't speed up the code so much.

1
2
3
4
5
6
7
for i= 1, #player(0, 'table') do
     local id = players[i]
end

for _, id in pairs(player(0, 'table')) do

end
And those both will go much much slower.

old Re: Which one is faster to be executed?

Mami Tomoe
User Off Offline

Quote
Well using this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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


I got these results for 1 player:
IMG:https://i.imgur.com/YXKeJIe.png


This for 12:
IMG:https://i.imgur.com/FRyGjSX.png


And this for a full 32 player server:
IMG:https://i.imgur.com/5WIZ701.png


Showing that my way was indeed, superior.
*drops mic*
edited 1×, last 04.06.20 06:56:27 pm

old Re: Which one is faster to be executed?

DC
Admin Off Offline

Quote
I'm actually surprised that user Mami Tomoe's approach to improve it doesn't give an even bigger performance boost.

I mean: This is 1 call to a very expensive CS2D method (
player(0, 'table'
) vs 32 calls to that method.
It should be obvious that it's faster to call it only one time.

The fact that this method returns a table and not just a single value should already make you think "this must be expensive. I should make sure to call it as few times as possible!".
(but of course methods with no or a simple return value can be expensive as well)

Using method calls in loops is bad for performance in general. Keep in mind that the method will be invoked for each iteration. It's always better to cache the values in a local variable (if you're sure that the values don't change during the loop).

old Re: Which one is faster to be executed?

Gaios
Reviewer Off Offline

Quote
I just tested and benchmarked it on not that fast PC, 1.6 GHz CPU clock (duo). Similar to actual VPS that people buy.

@user 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.

Summary
• There's no real difference in CS2D Lua scripts bettwen
for #players
and
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.

flame 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.

More >
edited 3×, last 05.06.20 01:12:09 am

old Re: Which one is faster to be executed?

MikuAuahDark
User Off Offline

Quote
Even in case if you can't cache player variables you need, like player rotation, health, or position, you can always localize
player
instead by adding
local player = _G.player
at top of your scripts. That helps in performance slightly because local variables accessed faster than global variable. Of course you're not limited to
player
function. You can do it to any functions.

Also reducing calls to CS2D Lua API absolutely help because that's most likely your bottleneck!
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview