10.12.2016, 16:03
(
Последний раз редактировалось Swedky; 10.12.2016 в 20:13.
Причина: Added "combination.inc" attachment.
)
Combinations
This include allows you to use a numeric combinations in a one single variable to store basic data, instead of using two or more ones, so you will be able to save a lot of memory (36 bytes for every var, to be exact).
Usage
This may be confusing at first, but it's simple.
By using this include, you can delete those variables which only store few amount of value, and store all them in a single one.
For example: using per-player vars (MAX_PLAYERS = 100)
It will eat 4,000 bytes ((10 * 100) * 4).
Now look at this, using numeric combinations:
It will eat just 400 bytes (a difference of 3,600 bytes). This may not be a huge difference, but image if you're using a lot of vars that only store, atleast, 0 and 1. It will help you saving a huge amount of memory.
This works by using that one single variable (in this case "pCombs", but you can use any variable) and save a value in one slot (or digit/index - as you want to call it) of the number.
Features
Increase/Decrease
Since by this way isn't possible to do ++/--, you must do this:
Benchmarks
Download
http://pastebin.com/mkcHuTrZ
This include allows you to use a numeric combinations in a one single variable to store basic data, instead of using two or more ones, so you will be able to save a lot of memory (36 bytes for every var, to be exact).
Usage
This may be confusing at first, but it's simple.
By using this include, you can delete those variables which only store few amount of value, and store all them in a single one.
For example: using per-player vars (MAX_PLAYERS = 100)
PHP код:
new var1[MAX_PLAYERS],
var2[MAX_PLAYERS],
var3[MAX_PLAYERS],
var4[MAX_PLAYERS],
var5[MAX_PLAYERS],
var6[MAX_PLAYERS],
var7[MAX_PLAYERS],
var8[MAX_PLAYERS],
var9[MAX_PLAYERS],
var10[MAX_PLAYERS];
// ...
var1[playerid] = 2;
var2[playerid] = 0;
var3[playerid] = 9;
var4[playerid] = 4;
var5[playerid] = 6;
var6[playerid] = 3;
var7[playerid] = 5;
var8[playerid] = 4;
var9[playerid] = 1;
var10[playerid] = 1;
// ...
printf("var 1: %i", var1[playerid]); // output "var 1: 2"
printf("var 2: %i", var2[playerid]); // output "var 2: 0"
printf("var 3: %i", var3[playerid]); // output "var 3: 9"
printf("var 4: %i", var4[playerid]); // output "var 4: 4"
printf("var 5: %i", var4[playerid]); // output "var 5: 6"
printf("var 6: %i", var4[playerid]); // output "var 6: 3"
printf("var 7: %i", var4[playerid]); // output "var 7: 5"
printf("var 8: %i", var4[playerid]); // output "var 8: 3"
printf("var 9: %i", var4[playerid]); // output "var 9: 1"
printf("var 10: %i", var4[playerid]); // output "var 10: 1"
Now look at this, using numeric combinations:
PHP код:
#include <combinations>
// Use defines or something for avoid confusions
#define COMBINATION_VAR_1 (0)
#define COMBINATION_VAR_2 (1)
#define COMBINATION_VAR_3 (2)
#define COMBINATION_VAR_4 (3)
#define COMBINATION_VAR_5 (4)
#define COMBINATION_VAR_6 (5)
#define COMBINATION_VAR_7 (6)
#define COMBINATION_VAR_8 (7)
#define COMBINATION_VAR_9 (8)
#define COMBINATION_VAR_10 (9)
new pCombs[MAX_PLAYERS];
// ...
SetCombination(pCombs[playerid], COMBINATION_VAR_1, 2);
SetCombination(pCombs[playerid], COMBINATION_VAR_2, 0);
SetCombination(pCombs[playerid], COMBINATION_VAR_3, 9);
SetCombination(pCombs[playerid], COMBINATION_VAR_4, 4);
SetCombination(pCombs[playerid], COMBINATION_VAR_5, 6);
SetCombination(pCombs[playerid], COMBINATION_VAR_6, 3);
SetCombination(pCombs[playerid], COMBINATION_VAR_7, 5);
SetCombination(pCombs[playerid], COMBINATION_VAR_8, 4);
SetCombination(pCombs[playerid], COMBINATION_VAR_9, 1);
SetCombination(pCombs[playerid], COMBINATION_VAR_10, 1);
// ...
printf("comb 1: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_1)); // output "comb 1: 2"
printf("comb 2: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_2)); // output "comb 2: 0"
printf("comb 3: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_3)); // output "comb 3: 9"
printf("comb 4: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_4)); // output "comb 4: 4"
printf("comb 5: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_5)); // output "comb 5: 6"
printf("comb 6: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_6)); // output "comb 6: 3"
printf("comb 7: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_7)); // output "comb 7: 5"
printf("comb 8: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_8)); // output "comb 8: 4"
printf("comb 9: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_9)); // output "comb 9: 1"
printf("comb 10: %i", GetCombination(pCombs[playerid], COMBINATION_VAR_10)); // output "comb 10: 1"
This works by using that one single variable (in this case "pCombs", but you can use any variable) and save a value in one slot (or digit/index - as you want to call it) of the number.
Features
- You only can save values between 0-9 for every slot.
- You only can use slots from 0-9 for every variable.
- You cannot use any tag in the variable.
- Doesn't support negative values.
- Support multi-dimensional and char array.
- Be careful, if numeric combination reachs over 2,147,483,647 (cellmax), you will get wrongs values. To avoid this at all, make sure 9th slot only save values between 0 and 1.
- This is a good and easy way to save/load player-settings!
Increase/Decrease
Since by this way isn't possible to do ++/--, you must do this:
PHP код:
new val = GetCombination(my_var, my_slot) + 1; // Increae
SetCombination(my_var, my_slot, val);
new val = GetCombination(my_var, my_slot) - 1; // Decrease
SetCombination(my_var, my_slot, val);
//
new val = GetCombination(my_var, my_slot);
SetCombination(my_var, my_slot, ++val); // Increase
new val = GetCombination(my_var, my_slot);
SetCombination(my_var, my_slot, --val); // Decrease
Код:
// 10,000 iterations [Set] Tick: 33ms (0.0033 ms) [Get] Tick: 24ms (0.0024 ms) [Set] Tick: 35ms (0.0035 ms) [Get] Tick: 18ms (0.0018 ms) [Set] Tick: 31ms (0.0031 ms) [Get] Tick: 18ms (0.0018 ms)
http://pastebin.com/mkcHuTrZ