Question about which is faster and less cpu usage?
#1

Hello guys today im going to ask you which method is better and faster? or maybe less CPU Usage

First Method
pawn Код:
new CuffTime[MAX_PLAYERS];
public OnFilterScriptInit()
{
    SetTimer("CuffTimer",1000,1);
    return 1;
}

CMD:cuff(playerid, params[])
{
    //All Cuff Code here!
    //cuf my own self and will be auto uncuffed in 25 seconds
    CuffTime[playerid] =25;
    SetPlayerSpecialAction(playerid, SPECIAL_ACTION_CUFFED);
    TogglePlayerControllable(playerid, 0);
    return 1;
}

forward CuffTimer()
public CuffTimer()
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(CuffTime[i] > 1)
        {
            CuffTime[i] --;
        }
        if(CuffTime[i] == 1)
        {  
            //All Auto-Uncuff code here!
            CuffTime[i] =0;
            SetPlayerSpecialAction(i, SPECIAL_ACTION_NONE);
            TogglePlayerControllable(i, 1);
        }
    }
    return 1;
}
OR

2nd Method
pawn Код:
new IsCuffed[MAX_PLAYERS];
public OnFilterScriptInit()
{
    return 1;
}

CMD:cuff(playerid, params[])
{
    //All Cuff Code here!
    //cuf my own self and will be auto uncuffed in 25 seconds
    SetPlayerSpecialAction(playerid, SPECIAL_ACTION_CUFFED);
    TogglePlayerControllable(playerid, 0);
    SetTimerEx( "CuffTimer", 25000, false,"i", playerid);
    IsCuffed[playerid] =1;
    return 1;
}

forward CuffTimer(playerid)
public CuffTimer(playerid)
{  
    //When Auto-Uncuffed get called
    SetPlayerSpecialAction(playerid, SPECIAL_ACTION_NONE);
    TogglePlayerControllable(playerid, 1);
    IsCuffed[playerid] =0;
    return 1;
}
Reply
#2

They both do different things !
edit
Didn see all the code but now:
The second method is the best way as your are not running a loop for all 500+ players.
Also running a lot of timers but at the same time is better then Running all of functions in 1 timer.

This has being discussed many times..
Reply
#3

Quote:
Originally Posted by park4bmx
Посмотреть сообщение
They both do different things !
what do you mean "They both do different things !" ? i know its different but. if you we're me. which method are you gonna use? or if there is a code much better than the one i have

EDIT

talking about the first one i did undef MAX_PLAYERS and define it again so i do have this at top of my script

pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYESR (32)
Reply
#4

Do some benchmarks.
Reply
#5

the first method, i.e. one 1000ms timer running + doing a plain MAX_PLAYERS loop, is far more efficient than having 500 timers, where the overhead is needless to mention.
may i suggest to change one part into: (2nd edit, there needs to be >0, not >1 lol)
pawn Код:
if(CuffTime[i] > 0)
        {
            CuffTime[i] --;
        }
        else //this wont change any result nor behavior here, but its stable and easier to read
        {
Reply
#6

Yeah the first method is only one timer. Though to be honest every server generally has a main player loop timer on a one second timer, so if you already have one just shove it in there.
Reply
#7

Quote:
Originally Posted by MP2
Посмотреть сообщение
Yeah the first method is only one timer. Though to be honest every server generally has a main player loop timer on a one second timer, so if you already have one just shove it in there.
actually i only have that code shown. but yes i got 1 second timer that the 1st method is already in there "i did shove it :P"
Reply
#8

Quote:
Originally Posted by pds2012
Посмотреть сообщение
what do you mean "They both do different things !" ? i know its different but. if you we're me. which method are you gonna use? or if there is a code much better than the one i have

EDIT

talking about the first one i did undef MAX_PLAYERS and define it again so i do have this at top of my script

pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYESR (32)
If you're not expecting more than 32 players on your server, the second one is okay. The first one might not be that useful, as you're calling this function every second with little profit. I also don't expect there will be 32 cuffed players each 25 seconds. Probably not more than 5? 5 timers are not that big of a deal.
Reply
#9

Quote:
Originally Posted by Basssiiie
Посмотреть сообщение
If you're not expecting more than 32 players on your server, the second one is okay. The first one might not be that useful, as you're calling this function every second with little profit. I also don't expect there will be 32 cuffed players each 25 seconds. Probably not more than 5? 5 timers are not that big of a deal.
He can use more then one local timer if he need big amount of checks for 'timer variable' - ***** and slice made timer fix just for this cases, anyway, using TimerEx is not good for that kind of use, i suggest to use variables and if he need alot's of timer variables he can just use more local timers and ofc, use fix timers plugin or inc
Reply
#10

Quote:
Originally Posted by Basssiiie
Посмотреть сообщение
If you're not expecting more than 32 players on your server, the second one is okay. The first one might not be that useful, as you're calling this function every second with little profit. I also don't expect there will be 32 cuffed players each 25 seconds. Probably not more than 5? 5 timers are not that big of a deal.
as i said above.
Quote:
Originally Posted by pds2012
Посмотреть сообщение
actually i only have that code shown. but yes i got 1 second timer that the 1st method is already in there in a big callback"i did shove it :P"
and as many people recommended it. i feel confident to use the first one.

and im not about sure how many server slots i will get soon. but im assuming to start at 32.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)