Making a function faster
#1

I've managed to do a function that renders any message and splits into next lines after a space was found starting from the 100th character of every line. The function itself was pretty fast, but I've added something that counts amount of color codes ({FFFFFF}) to carry the last one if found to the next line, this made the function extremly slow the way I made it (100k iterations = 27 Seconds); while I am not expert on this issue (yet) maybe we can work it out, this is the function:

pawn Код:
stock RenderMessage(top, color, const text[])
{
    new temp[156], tosearch = 0, colorint, posscolor, lastcol[12];
    new mess[356], colors, tempc; format(mess, 356, "%s",text);

    while(strlen(mess) > 0)
    {
        if(strlen(mess) < 140)
        {
            SendClientMessage(top, color, mess);
            break;
        }
   
        strmid(temp, mess, 0, 128);
        while(strfind(temp, "{", true) != -1)
        {
            tempc = strfind(temp, "{", true);
            if(temp[tempc+7] == '}')
            {
                colors ++;
                strdel(temp, tempc, tempc+7);
            }
            else
            {
                temp[tempc] = '0';
                continue;
            }
        }
        temp = "";
   
        if(strfind(mess," ",true,100+colors*8) != -1)
        {
            tosearch = strfind(mess," ",true,100+colors*8)+1;
            while(tosearch > 140)
            {
                colors --;
                tosearch = strfind(mess," ",true,100+colors*8)+1;
            }
        }
       
        if(strfind(mess,"{",true) != -1) //color codes detection , YAY
        {
            posscolor = strfind(mess,"{",true);

            if(mess[posscolor+7] == '}') //detected one color
                colorint = posscolor;
               
            while(strfind(mess,"{",true,colorint+1) != -1) //repeat until none are found
            {
                posscolor = strfind(mess,"{",true,colorint+1);
                if(posscolor > tosearch) //if next color will be on the other line, use last color found to render on the next line
                {
                    posscolor = colorint;
                    break;
                }
                if(mess[posscolor+7] == '}') //if found, then assign the color
                {
                    colorint = posscolor;
                }
                else
                {
                    posscolor = colorint; //else, leave the last color.
                    break;
                }
            }

            if(colorint == posscolor) //if the color position equals the one that was found
                strmid(lastcol,mess,colorint,colorint+8); //get the last used color string.
        }

        strmid(temp, mess, 0, tosearch);
        SendClientMessage(top, color, temp);
        strdel(mess, 0, tosearch);
        strins(mess, lastcol, 0); //insert last used color into the new line to be processed.


        temp = "";
        tosearch = 0;
        colors = 0;
    }
    return 1;
}
Inputs:

pawn Код:
RenderMessage(playerid, 0xFFFFFFFF, "{FFFF00}0123456789 0123456789 {FF0000}0123456789 0123456789 {00FF00}0123456789 0123456789 {0000FF}0123456789 0123456789 {FFFF00}0123456789 0123456789 {00FFFF}0123456789 0123456789 {FF6600}0123456789 0123456789 0123456789 {FF66FF}0123456789 {0066FF}0123456789 0123456789");
RenderMessage(playerid, 0xFFFFFFFF, "{FFFF00}0123456789 0123456789 {FF0000}0123456789 0123456789 {00FF00}0123456789 0123456789 {0000FF}0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 {FF6600}0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789");
RenderMessage(playerid, 0xFFFFFFFF, "{FFFF00}0123456789 0123456789 {FF0000}0123456789 0123456789 {00FF00}0123456789 0123456789 0123456789 0123456789 {FFFF00}0123456789 0123456789 0123456789 0123456789 {00FFFF}0123456789 0123456789 {FF0000}0123456789 0123456789 {0066FF}0123456789 0123456789 0123456789 0123456789 {FFFF00}0123456789 0123456789");
Output:



(the blank line is an empty message sent, not part of the function)


Код:
Input
RenderMessage(0, 0xFFFFFFFF, "{FFFF00}0123456789 0123456789 {FF0000}0123456789 0123456789 {00FF00}0123456789 0123456789 0123456789 0123456789 {FFFF00}0123456789 0123456789 0123456789 0123456789 {00FFFF}0123456789 0123456789 {FF0000}0123456789 0123456789 {0066FF}0123456789 0123456789 0123456789 0123456789 {FFFF00}0123456789 0123456789");

Outputs
Sent 10 messages in 3 MS
Sent 100 messages in 43 MS
Sent 1000 messages in 297 MS
Sent 10000 messages in 2772 MS
Sent 100000 messages in 27551 MS
(There's a RenderMessageToAll variation so RenderMessage wouldn't be used to send messages in a loop to (all) players.)
Reply


Messages In This Thread
Making a function faster - by CuervO - 10.02.2014, 20:32
Re: Making a function faster - by Mattakil - 10.02.2014, 20:38
Re: Making a function faster - by CuervO - 10.02.2014, 20:39
Re: Making a function faster - by Mattakil - 10.02.2014, 20:41
Re: Making a function faster - by CuervO - 10.02.2014, 21:07
Re: Making a function faster - by Pottus - 10.02.2014, 21:25
Re: Making a function faster - by CuervO - 10.02.2014, 21:27

Forum Jump:


Users browsing this thread: 1 Guest(s)