SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 28/06/2016, 07:33 PM   #11
Freedom.
Big Clucker
 
Join Date: Oct 2012
Location: Turkey
Posts: 130
Reputation: 8
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by PawnHunter View Post
PHP Code:
new pName[MAX_PLAYERS][MAX_PLAYER_NAME]; // global array

// OnPlayerConnect

GetPlayerName(playeridpNameMAX_PLAYER_NAME);


// Anywhere in the script

... pName[playerid
PHP Code:
// anywhere in the script

new pName[MAX_PLAYER_NAME];
GetPlayerName(playeridpNameMAX_PLAYER_NAME);
... 
pName 
which is more preferred first method or second? And why?
I think the second one safer than other. I have experienced with global array sometimes names can be mixed other player's name. You should use that as stock.
Freedom. is offline   Reply With Quote
Old 28/06/2016, 08:54 PM   #12
iKarim
Huge Clucker
 
Join Date: Oct 2015
Posts: 487
Reputation: 81
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by Freedom. View Post
I think the second one safer than other. I have experienced with global array sometimes names can be mixed other player's name. You should use that as stock.
Stock is a keyword to tell compiler to ignore the 'function is unused' warnings, anyways I never experienced problems with global arrays at all, I was just asking.
iKarim is offline   Reply With Quote
Old 28/06/2016, 11:35 PM   #13
PrO.GameR
Gangsta
 
PrO.GameR's Avatar
 
Join Date: Oct 2012
Posts: 738
Reputation: 121
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by PawnHunter View Post
PHP Code:
new pName[MAX_PLAYERS][MAX_PLAYER_NAME]; // global array

// OnPlayerConnect

GetPlayerName(playeridpNameMAX_PLAYER_NAME);


// Anywhere in the script

... pName[playerid
PHP Code:
// anywhere in the script

new pName[MAX_PLAYER_NAME];
GetPlayerName(playeridpNameMAX_PLAYER_NAME);
... 
pName 
which is more preferred first method or second? And why?
Thats again the argument of speed vs. memory, calling a variable or array is faster than calling a function, even a native one, so first one is faster, and second one uses less memory, then there is your personal reference of what path you choose to go.
__________________
Blueberry Prison Roleplay will be back soon!
Follow the forums for more information about opening day.

Forums
PrO.GameR is offline   Reply With Quote
Old 04/07/2016, 06:32 PM   #14
DavidBilla
Huge Clucker
 
DavidBilla's Avatar
 
Join Date: Aug 2013
Location: Planet Earth
Posts: 309
Reputation: 35
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

What's the equivalent code for this using memset or memcpy
Code:
new Var[MAX_PLAYERS][MAX_PLAYERS];
foreach(Player,i)
{
Var[i][playerid]=0;
}
I really cant figure out a way to do this
__________________
DavidBilla is offline   Reply With Quote
Old 04/07/2016, 07:32 PM   #15
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 DavidBilla View Post
What's the equivalent code for this using memset or memcpy
Code:
new Var[MAX_PLAYERS][MAX_PLAYERS];
foreach(Player,i)
{
Var[i][playerid]=0;
}
I really cant figure out a way to do this
Dude, memset won't help that code. That is absolutely horrible code as is. Your array by default is 250000 cells, which is 1000000 bytes, which is just dumb to have in PAWN.

You are using y_iterate already, learn how to use it's more advanced iterators and you can make that code a lot better.
__________________
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
Old 06/07/2016, 01:30 PM   #16
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 876
Reputation: 261
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

Quote:
Originally Posted by PawnHunter View Post
Wow nice.

Ok so I want to ask a question, is the memcpy method to reset an array faster than this?
PHP Code:
enum P_DATA {
    
p_money,
    
p_stuff,
    
p_whatever
}
new 
    
Player[MAX_PLAYERS][P_DATA], 
    
resetPlayer[P_DATA]
    ;

    
// anywhere in the code:
    
Player[playerid] = resetPlayer
Second one is FASTER for larger arrays whereas memcpy is faster for smaller arrays.

^ and the array size where they meet in performance depends on the CPU!

I did not mention anything about that in the tutorial is because the speed gain is insignificant.

If you do it with a normal loop, say it takes around 1000ms. If you do it with memcpy, you can do it in less than 20ms. If you do the direct array indexing, you may do it in 15ms.

Ask yourself, is the 5ms gain significant compared to the 980ms gain?
Deciding when to use memcpy and when to assign directly would be a unnecessary headache for such an insignificant improvement. Even if assigning directly was 2x faster than memcpy, it would be insignificant. Why? Check the numbers again!

The numbers are guesses but the actual measurements will yield the same results.
Yashas is offline   Reply With Quote
Old 06/07/2016, 01:36 PM   #17
K0P
Gangsta
 
K0P's Avatar
 
Join Date: Nov 2015
Location: *.*.*.*
Posts: 566
Reputation: 54
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

LOL i was about to create that tutorial!
__________________
OptionComment
GitHubAll of the scripts made by me can be found here.
Contact InfoClick here if you want to contact me.
Gift Me CS:GOIF you are kind enough to gift me CS:GO
K0P is offline   Reply With Quote
Old 06/07/2016, 01:44 PM   #18
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
Second one is FASTER for larger arrays whereas memcpy is faster for smaller arrays.

^ and the array size where they meet in performance depends on the CPU!

I did not mention anything about that in the tutorial is because the speed gain is insignificant.

If you do it with a normal loop, say it takes around 1000ms. If you do it with memcpy, you can do it in less than 20ms. If you do the direct array indexing, you may do it in 15ms.

Ask yourself, is the 5ms gain significant compared to the 980ms gain?
Deciding when to use memcpy and when to assign directly would be a unnecessary headache for such an insignificant improvement. Even if assigning directly was 2x faster than memcpy, it would be insignificant. Why? Check the numbers again!

The numbers are guesses but the actual measurements will yield the same results.
What are you even talking about?

50,000 iterations:



Part 1 => http://pastebin.com/zULdXUm9
Part 2 => http://pastebin.com/A8WaE6fv
Part 3 => http://pastebin.com/gX0Yv2Fw
Part 4 => http://pastebin.com/95kJi2a6
SickAttack is offline   Reply With Quote
Old 06/07/2016, 01:51 PM   #19
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 876
Reputation: 261
Default Re: Creative & smart scripting techniques - improve performance & readablity in one go

You misinterpreted literally everything in my reply. xD

I was comparing memcpy with "array1 = array2;".

Not with and without a loop. Obviously, in that case loop would be slower.

Let me make it more clear on the terms I used.

Normal Loop
Code:
for(new i = 0; i < 50000; i ++)
array[i] = 0;
Memcpy
Code:
memcpy(array, dummy, 0, 50000*4);
Direct assignment
Code:
array = dummy;
Yashas is offline   Reply With Quote
Old 06/07/2016, 02:26 PM   #20
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
You misinterpreted literally everything in my reply. xD

I was comparing memcpy with "array1 = array2;".

Not with and without a loop. Obviously, in that case loop would be slower.

Let me make it more clear on the terms I used.

Normal Loop
Code:
for(new i = 0; i < 50000; i ++)
array[i] = 0;
Memcpy
Code:
memcpy(array, dummy, 0, 50000*4);
Direct assignment
Code:
array = dummy;
That's old data. But what are you trying to achieve here? Normally setting variables is already fast.

You are kinda of a freak with performance and all that, it isn't always about performance in a production server, it's also about functionality, which should be put first. Without functionality and good gameplay, performance is nothing.

It's good that you share your tips and all that, but it would be nice to see you do something else. As long as performance is decent or reasonable, then I don't see the problem in leaving it like that. Server owners should focus more on the functionality.

This is all for server owners anyway.
SickAttack 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.