SA-MP Forums Archive
Better, faster function? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Better, faster function? (/showthread.php?tid=269734)



Better, faster function? - ||123|| - 17.07.2011

Let's say I need to get a player's vehicle ID two times. Which version would be more faster and better and less memory consuming?

Version 1:
pawn Код:
if(GetPlayerVehicleID(playerid) == CPcar)
{
    SetPlayerFacingAngle(playerid, 36.12);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP car!);
        return 1;
    }
}
if(GetPlayerVehicleID(playerid) == CPtruck)
{
    SetPlayerFacingAngle(playerid, 31.13);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP truck!);
        return 1;
    }
}
Version 2:
pawn Код:
new veh;
veh = GetPlayerVehicleID(playerid);
if(veh == CPcar)
{
    SetPlayerFacingAngle(playerid, 36.12);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP car!);
        return 1;
    }
}
if(veh == CPtruck)
{
    SetPlayerFacingAngle(playerid, 31.13);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP truck!);
        return 1;
    }
}
I would also like to know, that in such a situation, should I use 'if' or 'else if' or 'else', in the second condition?


Re: Better, faster function? - Mauzen - 17.07.2011

Well, its about what you want. Every function call takes some cpu time, so the first variant will be slower than the second one. Therefore it does not use a variable, so it uses less ram. So you have to decide if you want low cpu load or low ram useage, these are the common two options when optimizing code.
"else" is always a good thing to use when you are sure that only one condition will be true, as you dont have to check all the others then, but especially in situations like this i would recommend "switch", this should be more efficient than both "if" variants.


Re: Better, faster function? - ||123|| - 17.07.2011

What are the uses of the two, RAM and CPU usage, I mean what difference would it make on the server?


Re: Better, faster function? - Mauzen - 17.07.2011

A lot of used cpu time can cause the server to lag when the script hangs for some milliseconds in a function, while your script will probably never use more ram than available (a million variables are just 4MB RAM)
This does not mean waste as much ram as possible, but if you got the choice, like you do here, I would always prefer the less CPU/more RAM variant.


Re: Better, faster function? - MoroDan - 17.07.2011

Quote:
Originally Posted by ||123||
Посмотреть сообщение
Let's say I need to get a player's vehicle ID two times. Which version would be more faster and better and less memory consuming?

Version 1:
pawn Код:
if(GetPlayerVehicleID(playerid) == CPcar)
{
    SetPlayerFacingAngle(playerid, 36.12);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP car!);
        return 1;
    }
}
if(GetPlayerVehicleID(playerid) == CPtruck)
{
    SetPlayerFacingAngle(playerid, 31.13);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP truck!);
        return 1;
    }
}
Version 2:
pawn Код:
new veh;
veh = GetPlayerVehicleID(playerid);
if(veh == CPcar)
{
    SetPlayerFacingAngle(playerid, 36.12);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP car!);
        return 1;
    }
}
if(veh == CPtruck)
{
    SetPlayerFacingAngle(playerid, 31.13);
    if(IsPlayerAdmin(playerid))
    {
        SendClientMessage(playerid, This is a CP truck!);
        return 1;
    }
}
I would also like to know, that in such a situation, should I use 'if' or 'else if' or 'else', in the second condition?
This it is:
pawn Код:
switch(GetPlayerVehicleID(playerid))
{
    case CPcar:
    {
        SetPlayerFacingAngle(playerid, 36.12);
        if(IsPlayerAdmin(playerid))
        {
            SendClientMessage(playerid, "This is a CP car!");
            return 1;
        }
    }
    case CPtruck:
    {
        SetPlayerFacingAngle(playerid, 31.13);
        if(IsPlayerAdmin(playerid))
        {
            SendClientMessage(playerid, "This is a CP truck!");
            return 1;
        }
    }
}



Re: Better, faster function? - MrDeath537 - 17.07.2011

Nothing.


Re: Better, faster function? - ||123|| - 17.07.2011

Quote:
Originally Posted by ******
Посмотреть сообщение
Are "CPcar" and "CPtruck" defines (I hope not)? If they are then "switch" will work, if they're variables that won't even compile!

The variable method is much better, especially as it makes the code more flexible in the long run.
I understand, they're actually variables, so the pawno won't compile it.
You said "especially as it makes the code more flexible in the wrong run.". You aware that I was using it 'locally', I mean not globally at the top. Just wanted to know that as I find this information alot of useful. Because as the code will only be flexible when it's inside those brackets.

- My thanks to everyone who helped me in this topic.