13.06.2018, 10:42
Hi guys, I was wondering if (if/else if) in larger callbacks like OnDialogResponse with 400 if/else if statements caused more lag than switch. Is it really more efficient to use switch or is is there no difference?
#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);
}
1. Took 433ms | Average: 0.000000 ms pertime
2. Took 126ms | Average: 0.000000 ms pertime
V: 8
#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);
}
1. Took 313ms | Average: 0.000000 ms pertime
2. Took 822ms | Average: 0.000000 ms pertime
V: 8