Hot to speed up this function
#1

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

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
Reply
#3

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.
Reply
#4

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.
Reply
#5

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
Reply
#6

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.
Reply
#7

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.
Reply
#8

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.
Reply
#9

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--;
Reply
#10

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.
Reply
#11

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.
Reply
#12

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
Reply
#13

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.
Reply
#14

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)