English Which one is faster to be executed?

15 replies
Goto Page
To the start Previous 1 Next To the start
Up
The Dark Shadow
User
Offline Off
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
03.06.20 02:42:43 pm
Up
SQ
Moderator
Offline Off
Apparently you are wrong. table pairs in this case are 10X faster.

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
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.
03.06.20 03:33:30 pm
Up
Mami Tomoe
User
Offline Off
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:

Code:
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
04.06.20 01:32:11 pm
Up
The Dark Shadow
User
Offline Off
@user SQ: Thank you

@user Mami Tomoe: This one won't work, Thanks anyway ;p
04.06.20 01:35:08 pm
Up
Mami Tomoe
User
Offline Off
@user The Dark Shadow: what do you mean I use it quite often.
04.06.20 02:03:04 pm
Up
SQ
Moderator
Offline Off
@user Mami Tomoe: Yeah, it's totally incorrect code, that should never work by design. You should reconsider your coding path.
04.06.20 03:11:05 pm
Up
Mami Tomoe
User
Offline Off
Have you guys tried it even?
04.06.20 03:13:08 pm
Up
SQ
Moderator
Offline Off
This

Well the problem is solved anyway.
04.06.20 03:58:20 pm
Up
The Dark Shadow
User
Offline Off
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
04.06.20 04:38:36 pm
Up
Gaios
Security Supporter
Offline Off
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.
Code:
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.

Code:
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.
04.06.20 05:52:02 pm
Up
mrc
User
Offline Off
thats cool, I optimized all my scripts with those tips now.
CS2DBR Community - http://cs2dbr.tk
04.06.20 06:43:15 pm
Up
Mami Tomoe
User
Offline Off
Well using this code:
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
04.06.20 07:04:52 pm
Up
The Dark Shadow
User
Offline Off
@user Gaios: You are so popular

@user Mami Tomoe: Oh yeah, you are legendary
04.06.20 08:10:56 pm
Up
DC
Admin
Offline Off
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).
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
05.06.20 12:53:48 am
Up
Gaios
Security Supporter
Offline Off
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
05.06.20 05:48:38 am
Up
MikuAuahDark
User
Offline Off
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!
file cs2d LuaJIT for Dedicated Server (11) JIT POWER! | Know your Lua errors! | Draw stuff at Pixelize Art --xx-- server!
To the start Previous 1 Next To the start