Raised This Month: $ Target: $400
 0% 

set_task & player loop.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 06-11-2014 , 19:24   set_task & player loop.
Reply With Quote #1

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.
vamppa is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-11-2014 , 20:23   Re: set_task & player loop.
Reply With Quote #2

The answer to your question is dependent on what you are trying to do. There is no good generic answer to your question.
__________________
fysiks is offline
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 06-12-2014 , 00:55   Re: set_task & player loop.
Reply With Quote #3

Shit, was hoping to keep it simple. : p
vamppa is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 06-12-2014 , 06:04   Re: set_task & player loop.
Reply With Quote #4

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.
__________________
In Flames we trust!
Nextra is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 06-12-2014 , 06:17   Re: set_task & player loop.
Reply With Quote #5

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.
aron9forever is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 06-12-2014 , 06:24   Re: set_task & player loop.
Reply With Quote #6

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.
__________________
In Flames we trust!
Nextra is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 06-12-2014 , 06:37   Re: set_task & player loop.
Reply With Quote #7

Quote:
Originally Posted by Nextra View Post
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?
aron9forever is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 06-12-2014 , 07:19   Re: set_task & player loop.
Reply With Quote #8

Quote:
Originally Posted by aron9forever View Post
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 View Post
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.
__________________
In Flames we trust!
Nextra is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 06-12-2014 , 09:24   Re: set_task & player loop.
Reply With Quote #9

Quote:
Originally Posted by Nextra View Post
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
aron9forever is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-12-2014 , 09:30   Re: set_task & player loop.
Reply With Quote #10

Well you really shouldn't worry about a task that's looping once per 20 seconds. People are usually overexaggerating stuff like this.
Backstabnoob is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:50.


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