relations between arrays using ID.. loops.. acceptable?
#1

So, my gamemode currently uses quite a few loops (as necessary) to find & use relations between arrays.

For example, I preload HomeInfo[MAX_HOMES][enum] during OnGameModeInit() from a database, and PlayerStats[MAX_PLAYERS][enum] on player's successful login. PlayerStats has an enum "charID"... HomeInfo has "ownerID".

I then loop through the HomeInfo array to find relationships:

Код:
for(new i=0;i<MAX_HOMES;i++) { 
    if(HomeInfo[i][ownerID] == PlayerStats[playerid][charID) {
        DoSomething();
        break;
    }
}
Now I know this works, obviously, but I'm going to be using these loops fairly frequently.. Is this safe? Is it efficient? Should I be storing these relationships in some other way?
Reply
#2

That code is perfectly fine, and I use a similar loop "frequently" too and I haven't expected any problems. If you're dealing with multiple houses, this may be the only way to get the ID of a house owned by a specific player.. although I compare strings and not IDs. Probably IDs are a much faster way.

pawn Код:
new Name[24];
GetPlayerName(playerid, Name);
for(new i; i != MAX_HOUSES; i++)
{
    if(!strcmp(HouseInfo[i][Owner], Name) && strlen(HouseInfo[i][Owner]) > 0) // strcmp returns 0 if either string is empty
    {
        DoSomething();
        break;
    }
}
Either way it's OK, considering you probably don't have thousands of houses.
Reply
#3

What if I did have thousands of houses? How would you recommend future-proofing this script? Are there things I can do to make sure that we're as quick & efficient as possible?

NEW QUESTION:

I also from time to time need to find the lowest slot in an array that's "empty" (all zeros).. To do this I've been looping, again.. As such. Is this the most efficient way of finding the first empty slot in an array?

Код:
new newArrID;
for(new i=0;i<MAX_PROPERTIES;i++) {
    if(HomeInfo[i][homeID] == 0) { // I'm able to check for homeID being zero because this value is assigned by DB.
        newArrID = i;
        break;
    }
}
printf("Create home in array slot %i", newArrID);
Reply
#4

With thousands I mean "many" thousands, because that code will probably be the LAST (or not even an option) thing to consider if you're having problems with performance.

Have you tried benchmarking? It's an excellent way to check how many milliseconds your code may take to perform, when executed many times (it can be 100 or 100,000 consecutive runs).
Reply
#5

Quote:
Originally Posted by TheStreetsRP
Посмотреть сообщение
NEW QUESTION:

I also from time to time need to find the highest slot in an array that's "empty" (all zeros).. To do this I've been looping, again.. As such.

Код:
for(new i=0;i<MAX_PROPERTIEs;i++) {
    if(HomeInfo[i][homeID] == 0) { // I'm able to check for homeID being zero because this value is assigned by DB.
        newArrID = i;
        break;
    }
}
That is OK too. Consider that you're finding the FIRST available slot, and not the highest one, for that you need to do the loop "reversed".

Код:
for(new i=MAX_PROPERTIES;i>0;i--) {
    if(HomeInfo[i][homeID] == 0) { // I'm able to check for homeID being zero because this value is assigned by DB.
        newArrID = i;
        break;
    }
}
You are probably the kind of person that is too tidy with performance, but really, computer code runs fast unless it's a very complex mathematical algorithm or you're running heavy code constantly in a timer with low intervals. It's just for you to do benchmarking tests and see the results!
Reply
#6

I have not tried benchmarking, I probably should. Thanks for the link.

Hey, I see your response below this post. Thank you very much for your input. Very much appreciated. +rep'd!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)