Code:
#include <amxmodx>
/*
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 )
public plugin_init() {
register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");
register_clcmd("say /test", "clcmdTest");
}
new UserFlags[33];
#define DefineHasFlag(%0,%1) ( get_user_flags(%0) & %1 )
stock StockHasFlag(id, flag)
return get_user_flags(id) & flag;
public clcmdTest(id) {
new NumLoops = 100000000;
new hTimer1, hTimer2, hTimer3, hTimer4, hTimer5;
// Method 1, Result 5.307s. #4
hTimer1 = TimerStart();
for ( new i ; i < NumLoops ; i++ ) {
if ( get_user_flags(id) & ADMIN_BAN ) { }
}
TimerStop(hTimer1);
// Method 2, Result 5.281s. #3
hTimer2 = TimerStart();
for ( new i ; i < NumLoops ; i++ ) {
if ( DefineHasFlag(id, ADMIN_BAN) ) { }
}
TimerStop(hTimer2);
// Method 3, Result 8.088s. #5
hTimer3 = TimerStart();
for ( new i ; i < NumLoops ; i++ ) {
if ( StockHasFlag(id, ADMIN_BAN) ) { }
}
TimerStop(hTimer3);
// Method 4, Result 4.780s. #2
UserFlags[id] = get_user_flags(id);
hTimer4 = TimerStart();
for ( new i ; i < NumLoops ; i++ ) {
if ( UserFlags[id] & ADMIN_BAN ) { }
}
TimerStop(hTimer4);
// Method 5, Result 4.351s. #1
new UserHasFlag = UserFlags[id] & ADMIN_BAN;
hTimer5 = TimerStart();
for ( new i ; i < NumLoops ; i++ ) {
if ( UserHasFlag ) { }
}
TimerStop(hTimer5);
// Print the results
TimerServerPrint(hTimer1, _, _, "Timer1: %t");
TimerServerPrint(hTimer2, _, _, "Timer2: %t");
TimerServerPrint(hTimer3, _, _, "Timer3: %t");
TimerServerPrint(hTimer4, _, _, "Timer4: %t");
TimerServerPrint(hTimer5, _, _, "Timer5: %t");
}
/* 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);
}