Tiny But Super Optimizations
#21

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
(yeah i meant that most of the things in this thread are not optimzations they are just pretty printing)
Totally agreed
Reply
#22

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
Quote:
Originally Posted by Crayder
Посмотреть сообщение
This thread is nothing about your mess. It's about tiny optimizations that many people will find extremely useful in many circumstances.

For your mess, you just need to learn how to write code. You clearly got the brunt of it down, just keep paying attention to how it's supposed to be done. Learn some other languages too, that will actually help a lot. Seeing how things are done in multiple languages will give you a great baseline style.
-_- that was not i meant for your info i know how to script in c++(u dont need to teach me that),pawn(ofcourse ),SQL ,sort of python and little bit Java .I just said my codes are not pretty printed (yeah i meant that most of the things in this thread are not optimzations they are just pretty printing)
Ladies, you're both pretty.
Reply
#23

What is more optimised??
this
Quote:

CMD:lsa(playerid) return SendClientMessage(playerid,-1,"BLAHAAAAA");

or
Quote:

CMD:lsa
{
SendClientMessage(playerid,-1,"BLAHAAAAA");
return 1;
}

Reply
#24

Quote:
Originally Posted by Slice
Посмотреть сообщение
Ladies, you're both pretty.
We both are like brothers
Reply
#25

Quote:
Originally Posted by GhostHacker
Посмотреть сообщение
What is more optimised??
this

or
I don't think that will have any major optimization, which needs attention, but I prefer the style you mentioned, the first one.
Reply
#26

Quote:
Originally Posted by AmigaBlizzard
View Post
PHP Code:
enum TPlayerData
{
    
UserID,
    
Name[25],
    
Password[130],
    
IP[16],
    
GPCI[130],
    
PasswordAttempts,
    
bool:LoggedIn,
    
bool:InClassSelection,
    
// Player data
    
Money,
    
Score,
    
AdminLevel,
    
Speed
}
new 
APlayerData[MAX_PLAYERS][TPlayerData]; 
Consider this enum structure.

Most people do this when clearing all data in a player's account when a player disconnects or connects (to be sure the new player won't get data from the previous player):
PHP Code:
APlayerData[playerid][UserID] = 0;
APlayerData[playerid][Name][0] = 0;
APlayerData[playerid][Password][0] = 0;
APlayerData[playerid][IP][0] = 0;
APlayerData[playerid][GPCI][0] = 0;
APlayerData[playerid][PasswordAttempts] = 0;
APlayerData[playerid][LoggedIn] = false;
APlayerData[playerid][InClassSelection] = false;
APlayerData[playerid][Money] = 0;
APlayerData[playerid][Score] = 0;
APlayerData[playerid][AdminLevel] = 0;
APlayerData[playerid][Speed] = 0
While it's alot easier to do this:
PHP Code:
new temp[TPlayerData];
APlayerData[playerid] = temp
No need for hundreds of lines of code to reset your entire structure one variable at a time and no risk about forgetting to reset one of them.
Just reset them all at once.

I have no idea if it's faster and if it is, how much faster.
I never tested the speed of it, but it surely is less code and less risk to clear all your vars.
This is a proper way of doing it
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    for(new i = 0; i < sizeof(Players[]); i++) {
        Players[playerid][pInfo:i] = 0; // or whatever ur player array is
    }
    return 1;
}
Of course if you use that make sure that you have your saving above it so you don't clear the players data entirely.

But for this thread this is completely unnecessary just for the minimal performance difference, it's not worth changing or doing in your current or future game modes. Eh I could see someone using it if they just found out about this way, and want to use it for the nano second differences, but not changes to already made GM's unless their that hungry for the performance.
Reply
#27

Quote:
Originally Posted by Wolfe
View Post
This is a proper way of doing it
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    for(new i = 0; i < sizeof(Players[]); i++) {
        Players[playerid][pInfo:i] = 0; // or whatever ur player array is
    }
    return 1;
}
Of course if you use that make sure that you have your saving above it so you don't clear the players data entirely.

But for this thread this is completely unnecessary just for the minimal performance difference, it's not worth changing or doing in your current or future game modes. Eh I could see someone using it if they just found out about this way, and want to use it for the nano second differences, but not changes to already made GM's unless their that hungry for the performance.
Thats the wrong way to do it, amiga's way is faster, easier and well .. 'proper'.
What people not seem to understand is this topic first assumes(or should) you have a heavily repetitive code, I.E a loop in OPU (Which honestly you shouldn't, but for the sake of example ...) then, having just 100 nanosec saved is alot for a code that loops MAX_SOMETHING 40 times a second.
And also even tho speed might not matter, some of these tiny optimizations are better coding habits, for example I won't sacrifice readability for nano speeds speeds like this:
PHP Code:
for(new i=MAX_SOMETHING;--i!=-1;) //this was bit faster than normal one as I remember 
but I would still have this code instead of the 5 declaration one.
PHP Code:
    new    HouseI[MAX_HOUSES][HouseInfo],
        
IteratorHouses<MAX_HOUSES>,
        
Housebla[MAX_HOUSES],
        
Houseblabla[MAX_HOUSES],
        
Houseblablabla[MAX_HOUSES]; 
Same in the case of readability because of indentation and faster in case of optimization.
Reply
#28

Very nice, I've learned some really useful information
Reply
#29

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
^ this. For reference, this is the fastest way I found of checking if a number falls in a range:

pawn Код:
if ((var + (cellmin + 0)) < (cellmin + 10))
Is equivalent to:

pawn Код:
if (0 <= var< 10)
Of course, it is horrible, and only faster for constants as the compiler can eliminate most of the maths to generate just one subtraction (a negative addition) and one comparison. Don't ever do this though - there are MUCH better ways to optimise your code, which I think I should return just to write a tutorial on, as this unhealthy obsession with non-optimisations is largely my fault, and I am eternally sorry for that! I still have to rectify this mistake.

There are five steps to optimising your code:
  • Profile it to find the slow parts.
  • Stop there because you probably don't need to go any further.
  • Profile again because profiling.
  • Use a better algorithm, not a comma instead of a semi-colon.
  • Profile again to check that the change you made actually improved things.
Algorithms are the way to go. The worst coded, primitive, and ugly quicksort function will destroy a bubblesort function written using every combination of micro-optimisation imaginable. Heck, for any decent array size a naive quicksort implementation in simple PAWN will destroy a bubblesort implementation even in hand-tuned x86 assembly. THAT is the power of better algorithms.
As far as returns go just seeing you gives people lot of assurance on sa-mp and joy, really hope you'd stop by more and more if not returning for a period of time.

I don't really think anyone is really using this as reference to update their codes and optimize it, it just serves as a documentation so if someone wants to write new blocks of code he might take a look at and use some of these mentioned to write better code, some of them are interesting and significant in huge projects, some are obviously unnecessary and ugly.
But still this coupled with a good tutorial on good algorithms would make those released gamemodes hell lot more useful.
Reply
#30

Quote:
Originally Posted by PrO.GameR
Посмотреть сообщение
Your English is most perfect of all, I'm pretty sure "most slower" means faster in this case people.
And also please if you provide an optimization provide an speed comparison.
I don't care really what do you think about my english

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
^ this. For reference, this is the fastest way I found of checking if a number falls in a range:

pawn Код:
if ((var + (cellmin + 0)) < (cellmin + 10))
Is equivalent to:

pawn Код:
if (0 <= var< 10)
That's a curious data thanks for it!
Reply
#31

Quote:
Originally Posted by EnzoMetlc
Посмотреть сообщение
I don't care really what do you think about my english
Yeah? Well you should. He was trying to help you. PrO.GameR is right, the way you have that worded makes what you are saying really confusing, especially without results. Even I still don't know which one you are trying to say is faster.
Reply
#32

People like you make the scripting world a better place for newbies.
Reply
#33

I actually found this first post very interesting. I learned something about how the compiler works.
Reply
#34

i++ is slower than ++i :P
Reply
#35

Quote:
Originally Posted by Awide
Посмотреть сообщение
i++ is slower than ++i :P
Yeah but they don't do the same thing.
Reply
#36

That is not possible.

i++ and ++i use the same instruction. The only difference is the position of the instruction. There shouldn't be any difference in speed.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)