SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 06/07/2016, 02:33 PM   #21
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 876
Reputation: 261
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Probably misinterpretation once again.

In the topic I was talking about using memcpy vs assiging everything manually. Memcpy is nearly 50x+ (depends on the array and size) faster than assigning values manually. Also, its more neat and readable.

That performance improvement is significant especially when you have large arrays/enum arrays. It is a WIN-WIN scenario (readability + performance)!

Somebody asked if using memcpy was faster than assigning an array directly and I just gave an explanation.
Yashas is offline   Reply With Quote
Old 06/07/2016, 02:47 PM   #22
SickAttack
Godfather
 
Join Date: Dec 2013
Posts: 5,253
Reputation: 751
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by Yashas View Post
Probably misinterpretation once again.

In the topic I was talking about using memcpy vs assiging everything manually. Memcpy is nearly 50x+ (depends on the array and size) faster than assigning values manually. Also, its more neat and readable.

That performance improvement is significant especially when you have large arrays/enum arrays. It is a WIN-WIN scenario (readability + performance)!

Somebody asked if using memcpy was faster than assigning an array directly and I just gave an explanation.
Sorry to break this to you, but no-one in productional terms gives a damn about performance as long as it is decent and it works.

Start seeing things how they really are, and don't waste your time on insignificant improvements.

This is a bit ridiculous, will you ever talk about something else or will we keep seeing "faster" on each reply you create?

Setting variables, arrays, w/e is already fast. 50,000 iterations = 1ms.
SickAttack is offline   Reply With Quote
Old 06/07/2016, 03:26 PM   #23
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 876
Reputation: 261
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

I have no words for that.

The ideas mentioned in this thread improve readability too. Would you use a for loop when you can do it nicely with a switch in your code? Would you bloat your OnPlayerConnect when you can do it neatly in one line?

Are you going to use DCMD? Its not just performance, its about readability too and the ones mentioned in this topic are not really insignificant.

Of course, sometimes every reply I make will have something which makes the given code faster. I just tell it. I don't mention it in the thread because it is insignificant. When someone asks, I answer them so that I can help people learn. In fact, you will find explanations in most of my replies and they also contain extra information. The purpose is to help people understand better, in other words, give more insight into the issue/problem/idea.
Yashas is offline   Reply With Quote
Old 08/07/2016, 03:35 PM   #24
Uberanwar
Gangsta
 
Uberanwar's Avatar
 
Join Date: May 2012
Location: Malaysia
Posts: 766
Reputation: 59
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

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
Quote:
memcpy(pInfo[playerid], pInfo[MAX_PLAYERS], 0, sizeof(pInfo[])*4, sizeof(pInfo[]));
^ that one

Quote:
enum PlayerInfo
{
Pass[129],
Admin,
Helper,
Level,
Exp,
ExpLeft,
Score,
Wanted,
Prison,
Donator,

FactionID,
FactionTier,
FactionRank[30],

HouseOwner,
CarOwner,
BizOwner,

Weapon1,
Weapon1Ammo,
Weapon2,
Weapon2Ammo,
Weapon3,
Weapon3Ammo,

Material,
Drug,
C4,

HandMoney,
BankMoney,

Kills,
Deaths,

Arrest,
Arrested,

TicketFine,
Ticketted,

Hours,
Minutes,
Seconds,


FightStyle,
Mask,
SkinID
};

new pInfo[MAX_PLAYERS][PlayerInfo];
__________________
Developer of Asauth RPG/Cops and Robbers (2016-now)
(previously known as Street Kings Cops and Robbers, 2013-2015).

Facebook Page: facebook.com/samp.acnr
Uberanwar is offline   Reply With Quote
Old 09/07/2016, 01:08 AM   #25
Jefff
High-roller
 
Join Date: Dec 2007
Posts: 2,578
Reputation: 290
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

1.
Code:
new pInfo[MAX_PLAYERS + 1][PlayerInfo];
2. I don't know for what is here memcpy
Code:
pInfo[playerid] = pInfo[MAX_PLAYERS];
Jefff is offline   Reply With Quote
Old 12/07/2016, 12:16 AM   #26
Dayvison_
High-roller
 
Dayvison_'s Avatar
 
Join Date: Nov 2013
Posts: 1,162
Reputation: 161
Thumbs up Re: Creative & smart scripting techniques - improve performance & readablity in one go

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.
__________________
Dayvison_ is offline   Reply With Quote
Old 12/07/2016, 05:55 PM   #27
OneDay
Big Clucker
 
Join Date: Sep 2015
Posts: 158
Reputation: 54
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

new gPlayerInfo[MAX_PLAYERS][PlayerData];
new playerInfo[PlayerData];

gPlayerInfo[playerid]=playerInfo;

is not function memcpy is pawn faster than function memcpy
OneDay is offline   Reply With Quote
Old 13/07/2016, 09:34 PM   #28
Untonyst
Big Clucker
 
Untonyst's Avatar
 
Join Date: Dec 2015
Posts: 66
Reputation: 9
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

  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


__________________

GitHub account.
Untonyst is offline   Reply With Quote
Old 14/07/2016, 12:00 AM   #29
OneDay
Big Clucker
 
Join Date: Sep 2015
Posts: 158
Reputation: 54
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

the second code is wrong because ++ but just use strfind
OneDay is offline   Reply With Quote
Old 14/07/2016, 01:39 AM   #30
Crayder
High-roller
 
Crayder's Avatar
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,870
Reputation: 560
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by OneDay View Post
the second code is wrong because ++ but just use strfind
No, it's not wrong. Learn PAWN before trying to call someone out like that.
__________________
Those who deserve reputation, do not need to beg for it.
Also, don't expect the help you need when offering reputation, you'll just be attracting Rep Hunters.
Join SA-MP Discord!
Crayder 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 11:05 AM.


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