SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 14/07/2016, 02:23 PM   #31
OneDay
Huge Clucker
 
Join Date: Sep 2015
Posts: 293
Reputation: 87
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by Crayder View Post
No, it's not wrong. Learn PAWN before trying to call someone out like that.
i do know pawn but help for to advertise

Code:
new string[128] = "laa",
    i; // i = 0

while (string[i++] != '\0') // string[i] = 'l', i++ = 1
{
    if (string[i] == 'l') // i = 1, string[i] = 'a'
    {        
        // never done
        break;
    }
}

first 'l' missed
OneDay is offline   Reply With Quote
Old 15/07/2016, 11:29 AM   #32
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 864
Reputation: 290
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by Jefff View Post
1.
Code:
new pInfo[MAX_PLAYERS + 1][PlayerInfo];
2. I don't know for what is here memcpy
Code:
pInfo[playerid] = pInfo[MAX_PLAYERS];
memcpy is actually faster that direct assignment when your array is VERY BIG.

Quote:
Originally Posted by Uberanwar View Post
Hey Yashas, thank you for this useful & helpful knowledge you've shared to the community. keep it up

I'm having an error and a warning though, need help on this..

warning 229: index tag mismatch (symbol "pInfo")
error 032: array index out of bounds (variable "pInfo")

both on the same line which is

^ that one
I guess its because of the sizeof, try sizeof(pInfo[][]) or something similar. I have lost touch with PAWN and I do not remember. A bit of tweaking should fix the problem.


Quote:
Originally Posted by Day_ View Post
This
PHP Code:
new Float:Coords[5][4] = {
    {
0.00.10.20.3},
    {
1.01.11.21.3},
    {
2.02.12.22.3},
    {
3.03.13.23.3},
    {
4.04.14.24.3}
};
function(){
    new 
rand random(sizeof(Coords));
    foreach(new 
iPlayer)
    {
        
SetPlayerPos(playeridCoords[rand][0], Coords[rand][1], Coords[rand][2]);
        
SetPlayerFacingAngle(playeridCoords[rand][3]);
    }
    
CreatePickup(12Coords[rand][0], Coords[rand][1], Coords[rand][2]);
    
Create3DTextLabel("You are here", -1Coords[rand][0], Coords[rand][1], Coords[rand][2], 15.00);

is Slower than this
PHP Code:
new Float:Coords[5][4] = {
    {
0.00.10.20.3},
    {
1.01.11.21.3},
    {
2.02.12.22.3},
    {
3.03.13.23.3},
    {
4.04.14.24.3}
};
function(){
    new 
rand random(sizeof(Coords));
    new 
        
Float:X        Coords[rand][0],
        
Float:Y        Coords[rand][1],
        
Float:Z        Coords[rand][2],
        
Float:Ang    Coords[rand][3]
    ;
    foreach(new 
iPlayer)
    {
        
SetPlayerPos(playeridXYZ);
        
SetPlayerFacingAngle(playeridCoords[rand][3]);
    }
    
CreatePickup(12XYZ);
    
Create3DTextLabel("You are here", -1XYZ15.00);

The functions are example.

I think, because every time at Coords[*] is called, need load the value i and the value from Coords, if you use the array only a time no have differences between speed.

Sorry my bad english, someone smarter than me can explain better.
It is well explained here (Click) with proof.

Quote:
Originally Posted by OneDay View Post
new gPlayerInfo[MAX_PLAYERS][PlayerData];
new playerInfo[PlayerData];

gPlayerInfo[playerid]=playerInfo;

is not function memcpy is pawn faster than function memcpy
IIRC memcpy is faster when the array is pretty huge. The improvement is however negligible since either way its WAY WAY WAY faster than the loop method to reset an array (the speed difference b/w memcpy and direct assignment doesn't matter here - I have explained it in on of my previous replies to SickAttack).

Quote:
Originally Posted by Untonyst View Post
  1. If you are looking for a only one symbol, it is better to use a loop.
  2. If necessary to use a for loop for the whole string, it is not necessary to know the size of the string.


    PHP Code:
    new string[128] = "qw;lasdl;k1l;k2l;4k;"// :D
    for (new i/* none */ ;i++)
    {
        if (
    string[i] == 'l')
        {
            
    // do something
            // ...
            // loop is complete
            
    break;
        }
        
    // cycle is complete
        // if symbol is not found
        
    if (string[i] == '\0')
        {
            break;
        }

    or

    PHP Code:
    new string[128] = "qw;lasdl;k1l;k2l;4k;",
        
    i;

    while (
    string[i++] != '\0')
    {
        if (
    string[i] == 'l')
        {        
            
    // do something
            // ...
            // loop is complete
            
    break;
        }

  3. For more optimization you can use variables, so often not to refer to the array index.

    PHP Code:
    new string[128] = "qw;lasdl;k1l;k2l;4k;"
        
    c,    // is values of the array index 
        
    i     // index 


    while ((
    string[i++]) != '\0')
    {
        if (
    == 'l')
        {        
            
    // do something
            // ...
            // loop is complete
            
    break;
        }

  4. If the condition need to compare three numbers, it is possible to make it so
    PHP Code:
    new value 7;
    if (
    <= value <= 9)
    {
        
    // do something


Your loop is horribley slow compared to strfind. Strfind accepts a substring instead of a character but its still faster when you want to search for a character.

Check Natives are faster than PAWN code in this (Click)topic to know more.

Quote:
Originally Posted by OneDay View Post
the second code is wrong because ++ but just use strfind
1. strfind is faster than that code
2. His code has a bug. It skips a character.

Quote:
Originally Posted by Crayder View Post
No, it's not wrong. Learn PAWN before trying to call someone out like that.
His English isn't good lol

He wanted to say that there is a bug and that code wont work because of i++ but he said it in a completely wrong and misleading way.
Yashas is offline   Reply With Quote
Old 18/07/2016, 04:09 PM   #33
OneDay
Huge Clucker
 
Join Date: Sep 2015
Posts: 293
Reputation: 87
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

thank you
OneDay is offline   Reply With Quote
Old 16/07/2017, 03:31 PM   #34
Sasino97
High-roller
 
Sasino97's Avatar
 
Join Date: Dec 2010
Location: Italia
Posts: 1,359
Reputation: 85
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Thanks for the useful info in this thread
Sasino97 is offline   Reply With Quote
Old 24/11/2017, 12:11 PM   #35
][Noname][
Gangsta
 
][Noname]['s Avatar
 
Join Date: Dec 2010
Posts: 613
Reputation: 80
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

so strange there is no info about GetPlayerPing
GetPlayerPing=loop for 1000
__________________
Owner of Absolute Play and SAMP Addon
][Noname][ is offline   Reply With Quote
Old 17/12/2017, 02:52 PM   #36
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 864
Reputation: 290
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by SPAWN_METAL View Post
so strange there is no info about GetPlayerPing
GetPlayerPing=loop for 1000
Why would someone loop GetPlayerPing times?
Yashas is offline   Reply With Quote
Old 01/01/2018, 03:07 PM   #37
VincenzoDrift
Little Clucker
 
VincenzoDrift's Avatar
 
Join Date: Jun 2014
Posts: 45
Reputation: 3
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

I get a tag mismatch:
Code:
#include <a_samp>

enum PlayerInfo_t
{
      Name[MAX_PLAYER_NAME],
      MoneyInHand,
      BankMoney,
      Team,
      Skin,
      Kills,
      Deaths,
      WantedLevel,
      AdminLevel,
      Muted,
      Jailed,
      Frozen      
}
new PlayerInfo[MAX_PLAYERS + 1][PlayerInfo_t];

public OnGameModeInit()
{
      PlayerInfo[MAX_PLAYERS][Team] = -1;
      PlayerInfo[MAX_PLAYERS][Skin] = 1;
      PlayerInfo[MAX_PLAYERS][MoneyInHand] = 
      PlayerInfo[MAX_PLAYERS][BankMoney] = 
      PlayerInfo[MAX_PLAYERS][Kills] = 
      PlayerInfo[MAX_PLAYERS][Deaths] =
      PlayerInfo[MAX_PLAYERS][WantedLevel] = 
      PlayerInfo[MAX_PLAYERS][AdminLevel] =  0;
      PlayerInfo[MAX_PLAYERS][Muted] = true;
      PlayerInfo[MAX_PLAYERS][Jailed] =
      PlayerInfo[MAX_PLAYERS][Frozen] =  false;
      return 1;
}

public OnPlayerConnect(playerid)
{
      memcpy(PlayerInfo[playerid], PlayerInfo[MAX_PLAYERS], 0, sizeof(PlayerInfo[])*4, sizeof(PlayerInfo[]));
      return 1;
}
__________________
Sorry for my bad english
VincenzoDrift is offline   Reply With Quote
Old 15/01/2018, 10:53 AM   #38
][Noname][
Gangsta
 
][Noname]['s Avatar
 
Join Date: Dec 2010
Posts: 613
Reputation: 80
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by Yashas View Post
Why would someone loop GetPlayerPing times?
loop in GetPlayerPing
__________________
Owner of Absolute Play and SAMP Addon
][Noname][ is offline   Reply With Quote
Old 15/01/2018, 11:54 PM   #39
Kaperstone
High-roller
 
Kaperstone's Avatar
 
Join Date: May 2011
Location: Russia
Posts: 3,338
Reputation: 818
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Code:
public OnPlayerConnect(playerid)
{
      memcpy(PlayerInfo[playerid], PlayerInfo[MAX_PLAYERS], 0, sizeof(PlayerInfo[])*4, sizeof(PlayerInfo[]));
}
ayy, double tag mismatch
__________________
Kaperstone is offline   Reply With Quote
Old 31/01/2018, 05:55 PM   #40
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 864
Reputation: 290
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by Kaperstone View Post
Code:
public OnPlayerConnect(playerid)
{
      memcpy(PlayerInfo[playerid], PlayerInfo[MAX_PLAYERS], 0, sizeof(PlayerInfo[])*4, sizeof(PlayerInfo[]));
}
ayy, double tag mismatch
The reason for index tag mismatch is that the second dimension of the PlayerInfo array expects the index to have the tag PlayerInfo_t but the memcpy argument does not expect any tag.

Possible fixes:
  1. Code:
    public OnPlayerConnect(playerid)
    {
          memcpy(PlayerInfo[playerid][PlayerInfo_t:0], PlayerInfo[MAX_PLAYERS][PlayerInfo_t:0], 0, sizeof(PlayerInfo[])*4, sizeof(PlayerInfo[]));
    }
  2. Use playerInfo_t instead of PlayerInfo_t. This would resolve the warning because weak tags are convertible to _:.
  3. Do direct assignment:
    Code:
    PlayerInfo[playerid] = PlayerInfo[MAX_PLAYERS];

Thanks for pointing out.
Yashas is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for someone creative jameskmonger Everything and Nothing 15 16/08/2012 09:55 AM
How to improve performance[Windows XP/Vista/7] Kalroz Everything and Nothing 10 29/07/2012 06:28 AM
Do you guys know how you can improve lag - shooting..I want to improve. SpezzyBreeze General 9 26/12/2011 04:23 PM


All times are GMT. The time now is 09:10 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.