SA-MP Forums Archive
Optimize this code... - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Optimize this code... (/showthread.php?tid=546454)



Optimize this code... - Baltimore - 15.11.2014

Can i optimize this code?:

pawn Код:
stock SendMessage(playerid, phrase[])
{
    new Nom[MAX_PLAYER_NAME];
    new str[256], str2[256], phrase2[256];
    GetPlayerName(playerid, Nom, sizeof(Nom));
    if(strlen(phrase) > 70)
    {
         strmid(str2, phrase, 70, strlen(phrase));
         strmid(phrase2, phrase, 0, 70);
         format(str,sizeof(str),"%s dit : %s ..." ,Nom, phrase2);
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         format(str,sizeof(str),"* ... %s" ,str2);
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         return 1;
    }
    format(str,sizeof(str),"%s dit : %s" ,phrase, Nom);
    SendClientMessage(playerid, 0xFFFFFFFF,str);
    return 1;
}
Thx!


Re: Optimize this code... - sammp - 15.11.2014

Make the strings 144 cells big rather than 256 cells big - 256 cells is extremely slow and client messages only support 144 cells


Re: Optimize this code... - proSeryoga - 15.11.2014

new Nom[MAX_PLAYER_NAME], str[256], str2[256], phrase2[256];


Re: Optimize this code... - Kwarde - 15.11.2014

No proSeryoga, no.

Quote:

256 cells is extremely slow

That's a little bit overdone exorbitant haha. But yeah, it's really a waste of memory.
I also see you use max lengh 70 for both the phrases. So you could start calculating: 70 + (MAX_PLAYER_NAME) + ('Other characters in the format') = size of the array (the string).

pawn Код:
stock SendMessage(playerid, phrase[])
{
    new Nom[MAX_PLAYER_NAME];
    new str[105], str2[76], phrase2[70];
    GetPlayerName(playerid, Nom, sizeof(Nom));
    if(strlen(phrase) > 70)
    {
         strmid(str2, phrase, 70, strlen(phrase));
         strmid(phrase2, phrase, 0, 70);
         format(str,sizeof(str),"%s dit : %s ..." ,Nom, phrase2); //24 (player name) + 11 (' dit :   ...') + 70 = 105
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         format(str,sizeof(str),"* ... %s" ,str2); //6 ('* ... ' + 70 = 76)
         SendClientMessage(playerid, 0xFFFFFFFF,str);
         return 1;
    }
    format(str,sizeof(str),"%s dit : %s" ,phrase, Nom);
    SendClientMessage(playerid, 0xFFFFFFFF,str);
    return 1;
}