Hot to speed up this function -
ATomas - 13.05.2016
Hello,
any idea how to speed up this function?
Код:
stock GetPlayerHouses(playerid)
{
new count,name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
for(new h;h<sizeof(House);h++)
{
if(strcmp(name,House[h][house_owner],false) == 0) count++;
}
return count;
}
This function is to slow. I need faster.
Thanks for any help
Re: Hot to speed up this function -
BiosMarcel - 13.05.2016
Instead of doing a for loop , looping thru all the houses you could just enter all the houses the player owns into his user file / databank , so the big loop is unneccessary
Re: Hot to speed up this function -
ATomas - 13.05.2016
Quote:
Originally Posted by [Bios]Marcel
Instead of doing a for loop , looping thru all the houses you could just enter all the houses the player owns into his user file / databank , so the big loop is unneccessary
|
House owner can be changed when player is offline.
Work with files is slow.
Re: Hot to speed up this function -
Konstantinos - 13.05.2016
The only other way I can think of is after the player logins, execute the code from that function once to see if the player has any house and store the result to a variable (global per-player). When the player buys a house, increase by one and when he sells it, decrease by one.
Re: Hot to speed up this function -
ATomas - 13.05.2016
Quote:
Originally Posted by Konstantinos
The only other way I can think of is after the player logins, execute the code from that function once to see if the player has any house and store the result to a variable (global per-player). When the player buys a house, increase by one and when he sells it, decrease by one.
|
Yes I have done it, but this function is slow
Re: Hot to speed up this function -
Konstantinos - 13.05.2016
Reducing the loop will give an advantage as well. Have a global variable and when the server starts and loads the houses, assign to the variable how many houses were loaded. Then on the loop, go until the variable and not the sizeof of House array. Don't forget then to increase/decrease depending on adding/deleting a house.
Re: Hot to speed up this function -
ikey07 - 13.05.2016
I would store all houses which player owns at login in a new variable, and at the rest of the places, just loop through those variables not the whole houses.
Re: Hot to speed up this function -
ATomas - 13.05.2016
Quote:
Originally Posted by Konstantinos
Reducing the loop will give an advantage as well. Have a global variable and when the server starts and loads the houses, assign to the variable how many houses were loaded. Then on the loop, go until the variable and not the sizeof of House array. Don't forget then to increase/decrease depending on adding/deleting a house.
|
How? Can you show me example? Thank you.
Re: Hot to speed up this function -
Konstantinos - 13.05.2016
pawn Код:
// global:
new gLoaded_Houses;
// when houses are loaded (in each iteration that a house is stored to the array "House"):
gLoaded_Houses++;
// any other loop after the houses have been loaded (stored to variables):
for(new h; h < gLoaded_Houses; h++)
// if you have command to create house and it is created successfully:
gLoaded_Houses++;
// if you have command to delete house and it is deleted successfully:
gLoaded_Houses--;
Re: Hot to speed up this function -
ATomas - 13.05.2016
Quote:
Originally Posted by Konstantinos
pawn Код:
// global: new gLoaded_Houses;
// when houses are loaded (in each iteration that a house is stored to the array "House"): gLoaded_Houses++;
// any other loop after the houses have been loaded (stored to variables): for(new h; h < gLoaded_Houses; h++)
// if you have command to create house and it is created successfully: gLoaded_Houses++;
// if you have command to delete house and it is deleted successfully: gLoaded_Houses--;
|
And how it solves my problem? I need to count how much houses player have. This script does not work with player. I have constant number of houses.
Re: Hot to speed up this function -
Konstantinos - 13.05.2016
It would reduce the times the loop goes if the size of House array is let's say 500 and you have only 400 houses created. If you mean by "constant number of houses" that your array "House" is full then yes, it does nothing (you didn't mention it though).
The best suggestion however would be to use SQL instead and all you need is a simple query.
Re: Hot to speed up this function -
ATomas - 13.05.2016
Quote:
Originally Posted by Konstantinos
It would reduce the times the loop goes if the size of House array is let's say 500 and you have only 400 houses created. If you mean by "constant number of houses" that your array "House" is full then yes, it does nothing (you didn't mention it though).
|
A have fully inicialized array.
Quote:
Originally Posted by Konstantinos
The best suggestion however would be to use SQL instead and all you need is a simple query.
|
I'm not sure whether the QSL fast enough
Re: Hot to speed up this function -
PrO.GameR - 13.05.2016
There are a couple of ways
A) Using SQL and then using queries
B) Using foreach and multi dimensional iterators.
C) Loading everything on start, keeping the data in an array for each player.
I recommend A or B or both.
Re: Hot to speed up this function -
Crayder - 13.05.2016
If it were me, I'd be doing this inside a SQL database. You can count the player's houses using "select * where owner is playername". That would be super quick and simple.