Code Optimization
#1

I saw this thread > https://sampforum.blast.hk/showthread.php?tid=580289 , and i want to optimize my script with trick 5. Assigning multiple variables to the same value & using memset.

Is that mean if i change this:

Код:
stock ResetVariables(playerid)
{
PlayerCuffed[playerid] = 0;
PlayerCuffedTime[playerid] = 0;
... and about 100 more variables.
RegistrationStep[playerid] = 0;
return 1;
}
To this:

Код:
stock ResetVariables(playerid)
{
PlayerCuffed[playerid] =
PlayerCuffedTime[playerid] =
... and about 100 more variables.
RegistrationStep[playerid] = 0;
return 1;
}
i will optimize my script? P.S. This reset variables when player connects!
Reply
#2

I'm not sure if you do that will make a difference in the speed of the code.

It is not necessary, however you can save space and lines you making something like this

PHP код:
#include <a_samp>
enum myenum
{
    
PlayerCuffed,
    
PlayerCuffedTime,
    
RegistrationStep
};
new 
Player[MAX_PLAYERS][myenum];
ResetVariables(playerid)
{
    for(new 
myenum:imyenumi++)
    {
        
Player[playerid][i] = 0;
    }
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
ResetVariables(playerid);
    return 
1;

The three lines in the function reset variables will make all the work restart all the variables of enumeration(playerid).
Reply
#3

But look this thread, trick 5! https://sampforum.blast.hk/showthread.php?tid=580289
Reply
#4

I doubt you will see any serious improvement. That "trick" assigns a variable to another variable whereas you're assigning a constant to a variable. That's a huge difference. As for the enum thing:

PHP код:
static const EMPTY_VARS[myenum];
Player[playerid] = EMPTY_VARS
Very simple.
Reply
#5

I dont understand you... I dont need help for enum, this variables are defined on the top of the script like, ex:
new PlayerCuffed[MAX_PLAYERS];
new PlayerCuffedTime[MAX_PLAYERS];

I start this topic because im not sure about trick 5, i read this for that trick in that thread:
Quote:

Speed Tests:
Using memset to set all elements of an array (3D) of 100 elements to zero:363,367,372
Setting elements of an array (3D) of 100 elements to zero using for loop:6662,6642,6687

Using memset is 18 times faster than looping through an array.
I read this part:
Quote:

Code 1:
Code:
x = abc;
y = abc;
z = abc;
Code 2:
Code:
x =
y =
z = abc;

, and i need anyone to tell me is this faster 18 times? (If i change what i have written in 1st post.)
Reply
#6

The 18 times faster is related to memset and resetting parts or the complete array (still the fastest for a complete reset is ... see Vince reply)
But in your case you can only use the first part of "5. Assigning multiple variables to the same value & using memset"

If you want to change it, change it but
Quote:
Originally Posted by Vince
Посмотреть сообщение
I doubt you will see any serious improvement.
Just remember it for the future
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)