03.01.2012, 13:41
memset
Very fast way to fill an array/string using a neat assembly instruction to do the dirty work. Usage example:
Its real power is in larger arrays:
For testing purposes I made another function that looped through the array and set each slot, it executed only 44 times/ms!
Very fast way to fill an array/string using a neat assembly instruction to do the dirty work. Usage example:
pawn Код:
new
string[128]
;
// Fill the whole string with spaces
// NOTE: "sizeof(x) - 1" must be used on strings to preserve the last NULL character!
memset(string, ' ', sizeof(string) - 1);
// Put 20 "@" characters in the beginning of the string
memset(string, '@', 20);
// Put 5 "X" characters at index 5
memset(string[5], 'X', 5);
print(string);
pawn Код:
new
somePlayerArray[MAX_PLAYERS]
;
START_BENCH(2000);
{
// Fill the array with the value 500
memset(somePlayerArray, 500);
}
FINISH_BENCH("memset player array");
// Output: Bench for memset player array: executes, by average, 613.08 times/ms.
pawn Код:
stock memset(aArray[], iValue, iSize = sizeof(aArray)) {
new
iAddress
;
// Store the address of the array
#emit LOAD.S.pri 12
#emit STOR.S.pri iAddress
// Convert the size from cells to bytes
iSize *= 4;
// Loop until there is nothing more to fill
while (iSize > 0) {
// I have to do this because the FILL instruction doesn't accept a dynamic number.
if (iSize >= 4096) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 4096
iSize -= 4096;
iAddress += 4096;
} else if (iSize >= 1024) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 1024
iSize -= 1024;
iAddress += 1024;
} else if (iSize >= 256) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 256
iSize -= 256;
iAddress += 256;
} else if (iSize >= 64) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 64
iSize -= 64;
iAddress += 64;
} else if (iSize >= 16) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 16
iSize -= 16;
iAddress += 16;
} else {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 4
iSize -= 4;
iAddress += 4;
}
}
// aArray is used, just not by its symbol name
#pragma unused aArray
}