Code:
#include <amxmodx>
#include <engine>
/*
new hTimer = TimerStart();
// ...
TimerStop(hTimer);
server_print("Timer: %d days, %d hours, %d minutes, %d seconds and %d milliseconds.", TimerDays(hTimer), TimerHours(hTimer), TimerMinutes(hTimer), TimerSeconds(hTimer), TimerMilliseconds(hTimer));
*/
#define TimerStart() tickcount()
#define TimerMid(%0) ( tickcount() - %0 )
#define TimerStop(%0) ( %0 = tickcount() - %0 )
#define TimerDays(%0) ( %0 / 86400000 )
#define TimerHours(%0) ( %0 % 86400000 / 3600000 )
#define TimerMinutes(%0) ( %0 % 3600000 / 60000 )
#define TimerSeconds(%0) ( %0 % 60000 / 1000 )
#define TimerMilliseconds(%0) ( %0 % 1000 )
#define MAX_TURRETS 10
#define NUM_LOOPS 5000000
/* Method 1 */
new gType[33][MAX_TURRETS];
new gCanShoot[33][MAX_TURRETS];
new gAmmo[33][MAX_TURRETS];
new gTarget[33][MAX_TURRETS];
/* Method 2 */
enum TurretInfo {
_Type,
_CanShoot,
_Ammo,
_Target
}
new gTurretInfo[33][MAX_TURRETS][any:TurretInfo];
/* Method 3 */
new Array:gTurretArray[33];
/* Method 4 */
// No need for globals.
public plugin_init() {
register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");
register_clcmd("test", "test");
}
public test(id) {
/* Pre */
new iOwner, iTurret;
new Type, CanShoot, Ammo, Target;
entity_set_int(id, EV_INT_iuser1, id);
entity_set_int(id, EV_INT_iuser2, 3);
/* Method 1 */
new hTimer1 = TimerStart();
for ( new i ; i < NUM_LOOPS ; i++ ) {
iOwner = entity_get_int(id, EV_INT_iuser1);
iTurret = entity_get_int(id, EV_INT_iuser2);
Type = gType[iOwner][iTurret];
CanShoot = gCanShoot[iOwner][iTurret];
Ammo = gAmmo[iOwner][iTurret];
Target = gTarget[iOwner][iTurret];
}
TimerStop(hTimer1);
/* Method 2 */
new hTimer2 = TimerStart();
for ( new i ; i < NUM_LOOPS ; i++ ) {
iOwner = entity_get_int(id, EV_INT_iuser1);
iTurret = entity_get_int(id, EV_INT_iuser2);
Type = gTurretInfo[iOwner][iTurret][_Type];
Target = gTurretInfo[iOwner][iTurret][_Target];
Ammo = gTurretInfo[iOwner][iTurret][_Ammo];
CanShoot = gTurretInfo[iOwner][iTurret][_CanShoot];
}
TimerStop(hTimer2);
/* Method 3 */
gTurretArray[id] = ArrayCreate(sizeof gTurretInfo[][], 10);
for ( new i ; i < MAX_TURRETS ; i++ )
ArrayPushArray(gTurretArray[id], gTurretInfo[id][i]);
new hTimer3 = TimerStart();
for ( new i ; i < NUM_LOOPS ; i++ ) {
iOwner = entity_get_int(id, EV_INT_iuser1);
iTurret = entity_get_int(id, EV_INT_iuser2);
static aValues[4];
ArrayGetArray(gTurretArray[id], iTurret, aValues);
Type = aValues[0];
Target = aValues[1];
Ammo = aValues[2];
CanShoot = aValues[3];
}
TimerStop(hTimer3);
/* Method 4 */
new hTimer4 = TimerStart();
for ( new i ; i < NUM_LOOPS ; i++ ) {
iOwner = entity_get_int(id, EV_INT_iuser1);
iTurret = entity_get_int(id, EV_INT_iuser2);
Type = entity_get_int(id, EV_INT_iuser3);
Target = entity_get_int(id, EV_INT_iuser4);
Ammo = entity_get_int(id, EV_INT_flSwimTime);
CanShoot = entity_get_int(id, EV_INT_team);
}
TimerStop(hTimer4);
/* Post */
TimerServerPrint(hTimer1, 2, false, "Timer 1: %t");
TimerServerPrint(hTimer2, 2, false, "Timer 2: %t");
TimerServerPrint(hTimer3, 2, false, "Timer 3: %t");
TimerServerPrint(hTimer4, 2, false, "Timer 4: %t");
server_print("%s", 0*Type*Target*Ammo*CanShoot);
}
/* TimerFormat(hTimer, output[], maxlen, mode = 1, bool:full = 0)
* Formats the result of a timer handle into a string.
*
* Parameters:
*
* hTimer
* Timer Handle
*
* output[]
* The output string
*
* maxlen
* Maximum size of the output string
*
* mode
* 1: 00:00:00:00.000
* 2: 0d 0h 0m 0s 0ms
*
* bool:full
* If full is set to true it will print all fields, even those which contains no value.
* If full is set to false and mode is set to 1, it will print the first field that contains a value and everything after that point. For example: 03:00:00.295
* If full is set to false and mode is set to 2, it will print only the fields that contains a value. For example: 3h 295ms
*/
stock TimerFormat(hTimer, output[], maxlen, mode = 1, bool:full = false) {
new len;
if ( full || TimerDays(hTimer) )
len = formatex(output, maxlen, mode == 1 ? "%02d:" : "%dd ", TimerDays(hTimer));
if ( full || ( len && mode == 1 ) || TimerHours(hTimer) )
len += formatex(output[len], maxlen - len, mode == 1 ? "%02d:" : "%dh ", TimerHours(hTimer));
if ( full || ( len && mode == 1 ) || TimerMinutes(hTimer) )
len += formatex(output[len], maxlen - len, mode == 1 ? "%02d:" : "%dm ", TimerMinutes(hTimer));
if ( full || ( len && mode == 1 ) || TimerSeconds(hTimer) )
len += formatex(output[len], maxlen - len, mode == 1 ? "%02d." : "%ds ", TimerSeconds(hTimer));
if ( full || ! len || mode == 1 || TimerMilliseconds(hTimer) )
len += formatex(output[len], maxlen - len, mode == 1 ? "%03d" : "%dms", TimerMilliseconds(hTimer));
}
/*
* Prints the result of a timer handle along with optional parameters.
*
* Parameters:
*
* hTimer
* Timer Handle
*
* mode
* 1: 00:00:00:00.000
* 2: 0d 0h 0m 0s 0ms
*
* bool:full
* If full is set to true it will print all fields, even those which contains no value.
* If full is set to false and mode is set to 1, it will print the first field that contains a value and everything after that point. For example: 03:00:00.295
* If full is set to false and mode is set to 2, it will print only the fields that contains a value. For example: 3h 295ms
*
* szFormat[]
* The string containing the format. Place %t inside and it will be replaced by the timer output. Supports all other standard inputs as well (%d, %i, %s, %f)
*
* any:...
* Additional arguments to be formatted into the string.
*/
stock TimerServerPrint(hTimer, mode = 1, bool:full = false, szFormat[], any:...) {
new szTimer[16];
TimerFormat(hTimer, szTimer, charsmax(szTimer), mode, full);
new szMessage[256];
copy(szMessage, charsmax(szMessage), szFormat);
replace(szMessage, charsmax(szMessage), "%t", szTimer);
vformat(szMessage, charsmax(szMessage), szMessage, 5);
server_print(szMessage);
}