Optimize this code...
#1

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!
Reply
#2

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

new Nom[MAX_PLAYER_NAME], str[256], str2[256], phrase2[256];
Reply
#4

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;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)