This is efficient? -
StreK - 02.05.2017
Hello, I am making a system to get a random business, but I do not know if my code will be efficient.
In this I am looking for a business with certain characteristics so that it fits.
PHP код:
FindBusiness(playerid)
{
new i = random(ValidBusiness); // Total business
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= 1 && BusinessData[i][bizType] <= 4 && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
{
if (BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
{
print("found!");
return i;
}
}
print("search again.");
return FindBusiness(playerid);
}
Re: This is efficient? -
iLearner - 02.05.2017
PHP код:
print("search again.");
return FindBusiness(playerid);
This code will always be executed. to avoid it try it like this:
PHP код:
FindBusiness(playerid)
{
new i = random(ValidBusiness); // Total business
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= 1 && BusinessData[i][bizType] <= 4 && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
{
if (BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
{
print("found!");
return i;
}
}
else
{
print("search again.");
return FindBusiness(playerid);
}
}
But... if you're going through valid bussiness only then it should always
find it.
Respuesta: Re: This is efficient? -
StreK - 02.05.2017
Quote:
Originally Posted by iLearner
PHP код:
print("search again.");
return FindBusiness(playerid);
This code will always be executed. to avoid it try it like this:
PHP код:
FindBusiness(playerid)
{
new i = random(ValidBusiness); // Total business
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= 1 && BusinessData[i][bizType] <= 4 && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
{
if (BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
{
print("found!");
return i;
}
}
else
{
print("search again.");
return FindBusiness(playerid);
}
}
But... if you're going through valid bussiness only then it should always find it.
|
But the function must end with a return.
Re: This is efficient? -
iLearner - 02.05.2017
return 1;
Re: This is efficient? -
SyS - 02.05.2017
Quote:
Originally Posted by iLearner
PHP код:
print("search again.");
return FindBusiness(playerid);
This code will always be executed. to avoid it try it like this:
PHP код:
FindBusiness(playerid)
{
new i = random(ValidBusiness); // Total business
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= 1 && BusinessData[i][bizType] <= 4 && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
{
if (BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
{
print("found!");
return i;
}
}
else
{
print("search again.");
return FindBusiness(playerid);
}
}
But... if you're going through valid bussiness only then it should always find it.
|
Both are same and it won't get executed if the if statement get false. And if that become true it will return i and recursion part won't get executed.
Re: This is efficient? -
DRIFT_HUNTER - 02.05.2017
I would create new array. Loop thru businesses and save ones that fit to that new array. Then i would know HOW many of them fit criteria and i could use random function to chose one randomly...
Re: This is efficient? -
Duco - 02.05.2017
Well, it really depends on the amount of businesses you actually have vs. the amount of max businesses defined in the script.
If you have a max business value of 100, while you only have 15 valid businesses you'll have a chance of waiting a very long time before the randomizer finally hits one of the valid businesses, it can take up to minutes if not hours or days. I would rather grab a random value based on an enum length, so in this case you'll end up like this:
PHP код:
new i = random(sizeof(BusinessData));
I haven't tested it, but in theory this should work a lot more efficient, once I have the chance to do so I'll do some benchmarking tests comparing your method with mine.
Re: This is efficient? -
Logic_ - 02.05.2017
It would be better to store the current VALID businesses by making a variable and storing the amount in it. The amount could be increased in the loading and creation process. This would be better than using
new i = random(sizeof BusinessData); + There is a runtime error here (if I'm not wrong) << add
- 1 in the code.
Re: This is efficient? -
Vince - 02.05.2017
Quote:
Originally Posted by Duco
it can take up to minutes if not hours or days.
|
Theoretically, yes. Practically, no.
The most efficient way would still be a custom foreach iterator.
Respuesta: Re: This is efficient? -
StreK - 02.05.2017
Quote:
Originally Posted by Vince
Theoretically, yes. Practically, no.
The most efficient way would still be a custom foreach iterator.
|
I have an interator already, the thing is that I do not want a random business of the total business created, I want a random business with certain characteristics of them, for example: have a certain type or are located somewhere