[Tutorial] New Code Optimizations
#9

Quote:
Originally Posted by Gammix
Посмотреть сообщение
I think using arrays hardly matters, according to me its much readable than individual variables for each assignment (like your position example).

One thing i would really suggest people to limit defines (constants) to their exact usage limit. That would really save the memory and the output size.
For example: You can save alot of memory by just redefining MAX_PLAYERS.
pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS 100
Widely used in making player arrays; same for other stuff as well.
Thanks for reporting! Fixed it!

Using arrays matter a lot!!
If ****** were to be here he would have screwed you for that statement lol (because I remember him telling more organized doesn't mean more efficient)

At the MAX_PLAYERS statement, the memory you save depends more on your code. By the way no one cares about memory in 2015! But anyway it is good to keep memory usage minimum.

Quote:
Originally Posted by maximthepain
Посмотреть сообщение
Can you explain about when we should use stock or public and whats more optimized to which function?
ex:
Код:
public SavePlayerData(playerid)
or
Код:
stock SavePlayerData(playerid)
Or it doesn't matter in speed matters?
Also, you didn't speak about timers and OnPlayerUpdate alot of players use OnPlayerUpdate as a timer...
Also, does it matters if i'm using foreach function or for with MAX_PLAYERS in speed matters?

Overall i have learned alot more from this tutorial then ****** tutorial, I dont know why.
Calling a public or a stock produce identical assembly (AMX Code). Therefore the speeds will remain the same. But wait, public functions make the function accessible outside the script but that comes with a cost. More public functions you add to your script, the more slower CallLocalFunction & CallRemoteFunction & funcidx gets.

Hence, you must make functions public if and only if you are going to use them in CallLocalFunction/CallRemoteFunction/funcidx.

The stock keyword is optional, by prefixing a function identifier with stock only tells the compiler to not compile that function if it doesn't get called in the script.

About the OnPlayerUpdate, using OnPlayerUpdate is for timers with small interval is good instead of having to create shitlod of timers for each player. Each SetTimer/SetTimerEx you use , you make the server's overall performance less efficient and it will also take some memory. It is usually good to have one single timer (SetTimer/Ex) which checks all the players rather than having a timer for each individual player.

First of all if you are using MAX_PLAYERS, then stop using it and switch to GetPlayerPoolSize().When you have scattered playerids then foreach will be faster than using GetPlayerPoolSize Loop. But this never happens (extremely rare since SAMP gives the lowest available ID to the player who joins). GetPlayerPoolSize loop will be much faster than foreach when the player count is high, especially when the player count is nearly the max capacity.

@Cypress Gammix has answered but I would like to add some more to it. Use the subtraction loop if the order of iterations do not matter. I have never come across a situation where I had to check every player in order.

Quote:
Originally Posted by sammp
Посмотреть сообщение
stock
OT: Most of these optimizations only improve the execution time by milliseconds I'd bet, half of them aren't even worth the time to sit through and optimize. Put it this way: you won't need to do this in your scripts.
None of them improve execution time by milliseconds, in fact they are less than 0.1 milliseconds.Computers are very fast that you cannot observe the effect. You need to iterate a million times to see milliseconds. Please check my reply to Gammix below and let me know if you'd change your mind (half? or just 1 or 2 optimizations that are not worth optimizing).

Quote:
Originally Posted by sammp;
Actually, you're right. The variables are quicker than the array, sorry. But when using arrays it's much easier to contain and to sort. In scripting it's pretty much all about what gets the job done in the most readable manner. The difference in memory isnt even kbytes.
There is no question of memory here since local variables are stored on the stack. Moreover, both the array version and the individual variable version occupy the same amount of space on the stack. I think it pretty much clear that arrays are a way lot slower than individual variables after looking at the assembly output that I showed in the thread.If that does not convenience you, then here are few benchmark result:
Array:921,921,917
Non-Array:505,532,510

By the way I never told "not to use arrays". I just said avoid them when you can.

Examples:
Код:
new params[5];
new pos[3];
new color[2];
Quote:
Originally Posted by Gammix
Посмотреть сообщение
Yes in most of the case, why care about negligible speed!
I have no idea how many times I have told you that everything optimization makes negligible difference whereas collection of optimizations make a huge improvement.

Moreover, please justify "most of the case". I would prefer to use "few of them" make negligible improvement.

#1 I agree on this
#2 You just have no idea, it depends how many times you access the array.
Array (10 Assignments):2444,2448,2473
Non-Array (10 Assignments):972,975,963

Speed Test Code:http://pastebin.com/aMkNtaC2

#3 7x faster than using CallLocalFunction in my gamemode - hmm, how is this negligible?
#4 Anyone goes against this would have got badly screwed by ******
#5 Makes a difference when you hook SetPlayerHealth,SetPlayerPos,etc for Anti-Cheat or for some other purpose

#6 Check this
With Optimization:1102,1080,1069,1091
Without Optimization:2374,2359,2429,2364

Test Code:http://pastebin.com/SLZDGRG4

#7 I would agree on this but does make a small difference
With Optimization:134,128,129
Without Optimization:172,168,169

#8 This will improve performance to a very small extent but makes the code much more readable!

#9 You have no idea how slow floatsqrt is

#10 73.5 times faster than original code, negligible :O really?

#11 Sometimes twice as fast, sometimes thrice as fast! Negligible ye?

I have tested every optimization I have posted here whereas you made claims without checking.

If you are so least bothered about performance, then why not use dini,dcmds instead of y_ini/ZCMD?
Reply


Messages In This Thread
New Code Optimizations - by Yashas - 04.07.2015, 04:01
Re: New Code Optimizations - by Gammix - 04.07.2015, 09:59
Re: New Code Optimizations - by maximthepain - 04.07.2015, 10:33
Re: New Code Optimizations - by theYiin - 04.07.2015, 10:36
Re: New Code Optimizations - by Cypress - 04.07.2015, 10:42
Re: New Code Optimizations - by sammp - 04.07.2015, 10:51
Re: New Code Optimizations - by sammp - 04.07.2015, 10:59
Re: New Code Optimizations - by Gammix - 04.07.2015, 11:53
Re: New Code Optimizations - by Yashas - 04.07.2015, 15:49
Re: New Code Optimizations - by PT - 04.07.2015, 16:04
Re: New Code Optimizations - by Macluawn - 04.07.2015, 16:09
Re: New Code Optimizations - by Yashas - 04.07.2015, 16:17
Re: New Code Optimizations - by Gammix - 04.07.2015, 16:40
Re: New Code Optimizations - by Macluawn - 04.07.2015, 16:41
Re: New Code Optimizations - by Yashas - 04.07.2015, 16:53
Re: New Code Optimizations - by Macluawn - 04.07.2015, 16:56
Re: New Code Optimizations - by Yashas - 04.07.2015, 16:59
Re: New Code Optimizations - by Lynn - 04.07.2015, 17:17
Re: New Code Optimizations - by rymax99 - 04.07.2015, 17:52
Re: New Code Optimizations - by Gammix - 05.07.2015, 02:31
Re: New Code Optimizations - by Tamer - 05.07.2015, 19:50
Re: New Code Optimizations - by Stanford - 06.07.2015, 03:07
Re: New Code Optimizations - by Cypress - 06.07.2015, 20:31
Re: New Code Optimizations - by Yashas - 07.07.2015, 01:57
Re: New Code Optimizations - by Sime30 - 07.07.2015, 17:58
Re: New Code Optimizations - by kristo - 07.07.2015, 18:09
Re: New Code Optimizations - by Sime30 - 07.07.2015, 18:59
Re: New Code Optimizations - by JokeyL - 25.01.2016, 16:38
Re: New Code Optimizations - by iKarim - 25.01.2016, 16:45
Re: New Code Optimizations - by vannesenn - 16.06.2016, 23:38
Re: New Code Optimizations - by Crystallize - 16.06.2016, 23:43
Re: New Code Optimizations - by Gammix - 16.06.2016, 23:43
Re: New Code Optimizations - by Vince - 17.06.2016, 09:37
Re: New Code Optimizations - by Gammix - 17.06.2016, 10:02
Re: New Code Optimizations - by vannesenn - 17.06.2016, 14:38
Re: New Code Optimizations - by vannesenn - 17.06.2016, 21:23
Re: New Code Optimizations - by Yashas - 20.06.2016, 17:22
Re: New Code Optimizations - by Yashas - 21.07.2016, 13:23
Re: New Code Optimizations - by Kaperstone - 15.01.2018, 21:21
Re: New Code Optimizations - by Yashas - 31.01.2018, 17:42

Forum Jump:


Users browsing this thread: 4 Guest(s)