13.06.2018, 10:56
(
Last edited by jlalt; 13/06/2018 at 02:54 PM.
)
You won't really sense the difference under ondialogresponse, look at the speed test:
-> Result:
Yes switch is faster than if but not that much that you will sense by small numbers of checks / calls.
Can switch be slower than if?
Yes it can.
->
Result ->
[ fixed a mistake on the second speed test, I've wrote one dot in switch case statement rather than two, it became slower even. ]
PHP Code:
#include <a_samp>
public OnFilterScriptInit()
{
new v;
new tick = GetTickCount();
for(new i = 0; i < 1000000; i++)
{
if(i == 1)
{
v = 0;
}
else if(i == 2)
{
v = 1;
}
else if(i == 3)
{
v = 2;
}
else if(i == 4)
{
v = 3;
}
else if(i == 5)
{
v = 4;
}
else if(i == 6)
{
v = 5;
}
else if(i == 7)
{
v = 6;
}
else if(i == 8)
{
v = 7;
}
else
{
v = 8;
}
}
new took = GetTickCount() - tick;
printf("1. Took %dms | Average: %f ms pertime", took, (took / 1000000));
v = 0;
tick = GetTickCount();
for(new i = 0; i < 1000000; i++)
{
switch(i)
{
case 1:
{
v = 0;
}
case 2:
{
v = 1;
}
case 3:
{
v = 2;
}
case 4:
{
v = 3;
}
case 5:
{
v = 4;
}
case 6:
{
v = 5;
}
case 7:
{
v = 6;
}
case 8:
{
v = 7;
}
default:
{
v = 8;
}
}
}
took = GetTickCount() - tick;
printf("2. Took %dms | Average: %f ms pertime", took, (took / 1000000));
printf("V: %d", v);
}
PHP Code:
1. Took 433ms | Average: 0.000000 ms pertime
2. Took 126ms | Average: 0.000000 ms pertime
V: 8
Can switch be slower than if?
Yes it can.
->
PHP Code:
#include <a_samp>
public OnFilterScriptInit()
{
new v;
new tick = GetTickCount();
for(new i = 0; i < 1000000; i++)
{
if(i >= 1 && i <= 100)
{
v = 0;
}
else if(i >= 500 && i <= 1000)
{
v = 1;
}
else if(i > 1000 && i <= 1500)
{
v = 2;
}
else
{
v = 8;
}
}
new took = GetTickCount() - tick;
printf("1. Took %dms | Average: %f ms pertime", took, (took / 1000000));
v = 0;
tick = GetTickCount();
for(new i = 0; i < 1000000; i++)
{
switch(i)
{
case 1..100:
{
v = 0;
}
case 500..1000:
{
v = 1;
}
case 1001..1500:
{
v = 2;
}
default:
{
v = 8;
}
}
}
took = GetTickCount() - tick;
printf("2. Took %dms | Average: %f ms pertime", took, (took / 1000000));
printf("V: %d", v);
}
PHP Code:
1. Took 313ms | Average: 0.000000 ms pertime
2. Took 822ms | Average: 0.000000 ms pertime
V: 8