Simple Optimization help
#1

pawn Код:
CMD:jobstats(playerid, params[])
{
    new string1[40], string2[40], string3[40], string4[40], string5[40], string6[40];
   
    SendClientMessage(playerid, COLOR_YELLOW, "_______________________");
    format(string1, sizeof(string1), "Mechanic:       Level %i",PlayerInfo[playerid][pJobLvl1]);
    format(string2, sizeof(string2), "Trash Truck:   Level %i",PlayerInfo[playerid][pJobLvl2]);
    format(string3, sizeof(string3), "Taxi Driver:     Level %i",PlayerInfo[playerid][pJobLvl3]);
    format(string4, sizeof(string4), "Truck Driver:  Level %i",PlayerInfo[playerid][pJobLvl4]);
    format(string5, sizeof(string5), "Mail Delivery:  Level %i",PlayerInfo[playerid][pJobLvl5]);
    format(string6, sizeof(string6), "Arms Dealer:  Level %i",PlayerInfo[playerid][pJobLvl6]);
    SendClientMessage(playerid, COLOR_YELLOW, string1);
    SendClientMessage(playerid, COLOR_YELLOW, string2);
    SendClientMessage(playerid, COLOR_YELLOW, string3);
    SendClientMessage(playerid, COLOR_YELLOW, string4);
    SendClientMessage(playerid, COLOR_YELLOW, string5);
    SendClientMessage(playerid, COLOR_YELLOW, string6);
    return 1;
}

I was hoping somebody could help me optimize this set of code. I just wrote this as an example, But I was thinking of ways of optimizing it down for learning purposes to a single string and a single 'SendClientMessage' to produce the same result. None of my theories are working exactly as I thought they would. Any help please? I'm sure it is a simple thing I am missing myself. Thanks in advance!
Reply
#2

As far as I know, there's no way of having SendClientMessage send a message that exceeds a string size of 128, so you'll need all of those SendClientMessage lines (though I could be wrong). But a way to optimize it is reusing the string variable for each line:

pawn Код:
CMD:jobstats(playerid, params[])
{
    new string[40];
    SendClientMessage(playerid, COLOR_YELLOW, "_______________________");
    format(string, sizeof(string), "Mechanic:       Level %i",PlayerInfo[playerid][pJobLvl1]);
        SendClientMessage(playerid, COLOR_YELLOW, string);
    format(string, sizeof(string), "Trash Truck:   Level %i",PlayerInfo[playerid][pJobLvl2]);
        SendClientMessage(playerid, COLOR_YELLOW, string);
    format(string, sizeof(string), "Taxi Driver:     Level %i",PlayerInfo[playerid][pJobLvl3]);
        SendClientMessage(playerid, COLOR_YELLOW, string);
    format(string, sizeof(string), "Truck Driver:  Level %i",PlayerInfo[playerid][pJobLvl4]);
        SendClientMessage(playerid, COLOR_YELLOW, string);
    format(string, sizeof(string), "Mail Delivery:  Level %i",PlayerInfo[playerid][pJobLvl5]);
        SendClientMessage(playerid, COLOR_YELLOW, string);
    format(string, sizeof(string), "Arms Dealer:  Level %i",PlayerInfo[playerid][pJobLvl6]);
        SendClientMessage(playerid, COLOR_YELLOW, string);
    return 1;
}
Reply
#3

"pJobLvl1, pJobLvl2, pJobLvl3, pJobLvl4, pJobLvl5, pJobLvl6...."

Why didn't you make an array for this ?
Reply
#4

Quote:
Originally Posted by Dutheil
Посмотреть сообщение
"pJobLvl1, pJobLvl2, pJobLvl3, pJobLvl4, pJobLvl5, pJobLvl6...."

Why didn't you make an array for this ?
Normal vars are faster than arrays.
Reply
#5

Quote:
Originally Posted by K0P
Посмотреть сообщение
Normal vars are faster than arrays.
Yes but in this case, an array will be better just for the organization !
Reply
#6

Thank you guys for the suggestions. I really appreciate it. And @Dutheil thank you for that, I didn't even consider doing that. Will definitely make it look a lot nicer! Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)