Better, faster function?
#1

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

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

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

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

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

Nothing.
Reply
#7

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


Forum Jump:


Users browsing this thread: 2 Guest(s)