26.10.2013, 13:07
Hello guys! I want to reduce CPU usage a little, and I decided to start with optimising loops. Now, in the script I'm working on there are tons of loops going through vehicles, your good ol' pal classic vehicle for loop:
Now, that's 2000 iterations, even when on server there is single vehicle only.
Let's go through my current code (really simplified)
RemoveVehicle has some more stuff to do, and the CarInfo is in fact 2d enumerated array, but that doesn't matter at the moment. Now, did I cover all possible vehicle creation moments? Also, are there any more places where vehicle can get destroyed? Does the vehicle after respawn reclaim its previous id, or it gets assigned a new one? Does SetVehicleToRespawn assign a new vehicle id, or it stays the same? Are there any serious flaws in my code? I'll test everything extensively, but with more eyes stuff is spotted easier.
pawn Код:
for(new v = 1; v != (MAX_VEHICLES + 1); ++v)
Let's go through my current code (really simplified)
pawn Код:
static
CarInfo[MAX_VEHICLES],
Iterator:CarInfo<MAX_VEHICLES>;
stock CST_CreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay)
{
new
vehicle = INVALID_VEHICLE_ID;
vehicle = CreateVehicle(modelid, x, y, z, angle, color1, color2, respawn_delay);
Iter_Add(CarInfo, vehicle);
return vehicle;
}
#if defined _ALS_CreateVehicle
#undef CreateVehicle
#else
#define _ALS_CreateVehicle
#endif
#define CreateVehicle CST_CreateVehicle
stock CST_AddStaticVehicle(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2)
{
new
vehicle = INVALID_VEHICLE_ID;
vehicle = AddStaticVehicle(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2);
Iter_Add(CarInfo, vehicle);
return vehicle;
}
#if defined _ALS_AddStaticVehicle
#undef AddStaticVehicle
#else
#define _ALS_AddStaticVehicle
#endif
#define AddStaticVehicle CST_AddStaticVehicle
stock CST_AddStaticVehicleEx(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2, respawn_delay)
{
new
vehicle = INVALID_VEHICLE_ID;
vehicle = AddStaticVehicleEx(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2, respawn_delay);
Iter_Add(CarInfo, vehicle);
return vehicle;
}
#if defined _ALS_AddStaticVehicleEx
#undef AddStaticVehicleEx
#else
#define _ALS_AddStaticVehicleEx
#endif
#define AddStaticVehicleEx CST_AddStaticVehicleEx
stock CST_DestroyVehicle(vehicleid)
{
RemoveVehicle(vehicleid);
return DestroyVehicle(vehicleid);
}
#if defined _ALS_DestroyVehicle
#undef DestroyVehicle
#else
#define _ALS_DestroyVehicle
#endif
#define DestroyVehicle CST_DestroyVehicle
stock RemoveVehicle(vehicleid) {
Iter_Remove(CarInfo, vehicleid);
}
public OnVehicleDeath(vehicleid, killerid)
{
RemoveVehicle(vehicleid);
return 1;
}