AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   set_task & player loop. (https://forums.alliedmods.net/showthread.php?t=241960)

vamppa 06-11-2014 19:24

set_task & player loop.
 
Lo

Im wondering what is more efficient when you need to do something frequent on each player.
The frequency is about every 20 seconds.

- Setting a global set_task then loop through all the players do code.
Doing one task still requires the CPU to check every cycle if it is time to do the task then cycle through all players.

- Seting a set task on ID, which could result to have 4 tasks being set at a given time, avoids using player loop.

This is a question without knowing what code the function will be handling so I'm expecting an answer excluding that.
I'm also wondering if get game time with a forward think would be more efficient instead, so if you got answers on that they are welc0me.

fysiks 06-11-2014 20:23

Re: set_task & player loop.
 
The answer to your question is dependent on what you are trying to do. There is no good generic answer to your question.

vamppa 06-12-2014 00:55

Re: set_task & player loop.
 
Shit, was hoping to keep it simple. : p

Nextra 06-12-2014 06:04

Re: set_task & player loop.
 
Running something every 20 seconds is far from frequent. From a performance standpoint either way would be fine, and you don't need to worry about this small-scale optimization at all.

Then again: Both ways will actually achieve a different thing. If you need a global checkpoint every 20 seconds where you may need to take action on specific players it is not the same as running 20 second tasks on a player when necessary.

If you install individual tasks depending on different events they will usually end up offset from each other. So task for player 1 may run at 10, 30, 50 seconds and task for player 2 at 20, 40, 60.

aron9forever 06-12-2014 06:17

Re: set_task & player loop.
 
The answer is simple and at the same time complicated as fawk. Many people agree that the more tasks you have, the bigger the cpu consumption. But this kind of thing applies to hundreds, maybe thousands of tasks, far from what someone would use in a common plugin.

To make a good example, in my server, when saving user information in database I use a player specific task, because I don't need it do execute at the same time for everyone and it gives me the power to set frequency depending on player.
I don't use looping tasks that execute on all users because I don't need them, for setting things like models, hud info I use forwards because they're more reliable and useful.
The only example of looping task that executes on all players I can give are the server chat advertisements.

Nextra 06-12-2014 06:24

Re: set_task & player loop.
 
If you're using so many tasks that the AMXX core itself has trouble executing them a small player loop is your least concern. In the general way he has asked this question it is diffcult (if not wrong) to advise him on any particular performance issues at all.

Constantly spelling out these worst-case scenarios only drives coders to do pointless optimizations and write bad code as a result.

The only answer here is: Use the right tool for the job, don't worry about minute performance differences.

aron9forever 06-12-2014 06:37

Re: set_task & player loop.
 
Quote:

Originally Posted by Nextra (Post 2150367)
The only answer here is: Use the right tool for the job, don't worry about minute performance differences.

Yeah I did that kind of stuff
Now that I've reached line 6000 in my core plugin and the server started spiking and players started getting kicked I kinda regret doing so

Teaching people bad habits because it's easier is bad, mkay?

Nextra 06-12-2014 07:19

Re: set_task & player loop.
 
Quote:

Originally Posted by aron9forever (Post 2150378)
Yeah I did that kind of stuff
Now that I've reached line 6000 in my core plugin and the server started spiking and players started getting kicked I kinda regret doing so

There are way larger and very complex plugins that don't cause lagspikes and player kicks. You're either doing very performance intensive stuff or you're doing something fundamentally wrong.

Can you please understand that this is completely unnecessary and way out of scope for this question? Just because you have once written code that required thorough optimization doesn't mean that you should spell doom for everyone and encourage newbies to write code that is needlessly complex and difficult.

There are basic optimizations that you should always recommend. But the hunt for microoptimizations in this community is a frequent cause for misunderstanding and mistakes because people constantly try to prematurely optimize perfectly fine code.

Just now I'm posting in threads that involve mishandling of caches and misuse of the static keyword. This is the kind of stuff I'm trying to avoid. There is a point and a time for optimizing problematic code. Running a task every 20 seconds is not that point and not that time.

Quote:

Originally Posted by aron9forever (Post 2150378)
Teaching people bad habits because it's easier is bad, mkay?

So you are saying it is a bad habit to use a task on a player that runs every 20 seconds. I really hope you are joking.

aron9forever 06-12-2014 09:24

Re: set_task & player loop.
 
Quote:

Originally Posted by Nextra (Post 2150400)
So you are saying it is a bad habit to use a task on a player that runs every 20 seconds. I really hope you are joking.

Take a chill pill, I'm not talking about this exact scenario, I'm just saying you shouldn't brush off optimizations like these instantly by telling people they don't need to worry about it

Backstabnoob 06-12-2014 09:30

Re: set_task & player loop.
 
Well you really shouldn't worry about a task that's looping once per 20 seconds. People are usually overexaggerating stuff like this.


All times are GMT -4. The time now is 09:39.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.