03.12.2010, 10:24
(
Последний раз редактировалось xxmitsu; 03.12.2010 в 10:39.
)
Function 1
vs function 2:
Timing for 100000 function calls with parameter 1:
Timing for 100000 function calls with parameter 16:
The execution time in case 1, will grow as long as the number of options(cases - which will have to be compared) will grow.
In the second implementation time remains the same, no matter how many options because the direct acces of the value in the array.
So, first is best for few options and second is for many options as perhaps > 50.
pawn Код:
stock FactionColor(factiune)
{
switch(factiune)
{
case 1..3: return COLOR_DBLUE;
case 4: return COLOR_LIGHTRED;
case 5: return COLOR_ORANGE;
case 6: return COLOR_GREEN;
case 7: return COLOR_LIGHTGREEN;
case 8: return COLOR_RED;
case 9: return COLOR_NEWS2;
case 10: return COLOR_YELLOW;
case 11: return COLOR_LICENSE;
case 12: return COLOR_GANGSTA;
case 13: return COLOR_BLOODS;
case 14: return COLOR_SURENOS;
case 15: return COLOR_NORTENOS;
case 16: return COLOR_FMA;
}
return COLOR_WHITE;
}
pawn Код:
stock FactionColor2(factiune)
{
static const
sc_culoare[17] =
{
COLOR_WHITE,
COLOR_DBLUE,
COLOR_DBLUE,
COLOR_DBLUE,
COLOR_LIGHTRED,
COLOR_ORANGE,
COLOR_GREEN,
COLOR_LIGHTGREEN,
COLOR_RED,
COLOR_NEWS2,
COLOR_YELLOW,
COLOR_LICENSE,
COLOR_GANGSTA,
COLOR_BLOODS,
COLOR_SURENOS,
COLOR_NORTENOS,
COLOR_FMA
};
if (0 <= factiune <= 16)
{
return sc_culoare[factiune];
}
return sc_culoare[0];
}
Код:
[13:30:58] GetTickCount = 43912 [13:30:58] Time #1: 13 [13:31:01] GetTickCount = 46336 [13:31:01] Time #2: 19
Код:
[13:41:31] GetTickCount = 13094 [13:41:31] Time #1: 16 [13:41:33] GetTickCount = 15054 [13:41:33] Time #2: 19
In the second implementation time remains the same, no matter how many options because the direct acces of the value in the array.
So, first is best for few options and second is for many options as perhaps > 50.