Fastest String Loop -
SickAttack - 11.10.2016
I posted this thread as most people create their loops that go through strings as follows:
for(new j = 0, k = strlen(string); j < k; j ++)
Which ended up in 3rd.
----------------------------------------------------
***** 1,000,000 ITERATIONS *****
// ** Lowest: 2,666 ms | Highest: 2,724 ms
Код:
new j = (strlen(string) - 1);
while(j != -1)
{
j --;
}
// ** Lowest: 2,648 ms | Highest: 2,700 ms
Код:
new j, k = strlen(string);
while(j != k)
{
j ++;
}
// ** Lowest: 2,619 ms | Highest: 2,686 ms
Код:
new j = -1;
while(string[++ j])
// ** Lowest: 2,536 ms | Highest: 2,599 ms
Код:
new j = strlen(string);
do
{
j --;
}
while(j != 0);
// ** Lowest: 1,428 ms | Highest: 1,450 ms
Код:
for(j = 0; string[j]; j ++)
// ** Lowest: 1,422 ms | Highest: 1,440 ms
Код:
for(new j = 0; string[j] != EOS; j ++)
// ** Lowest: 1,320 ms | Highest: 1,351 ms
Код:
for(new j = (strlen(string) - 1); j > -1; j --)
// ** Lowest: 1,314 ms | Highest: 1,349 ms
Код:
for(new j = strlen(string); --j != -1;)
// ** Lowest: 1,309 ms | Highest: 1,319 ms
Код:
for(new j = 0, k = strlen(string); j < k; j ++)
// ** Lowest: 1,299 ms | Highest: 1,324 ms
Код:
for(new j = 0, k = strlen(string); j != k; j ++)
// ** Lowest: 1,193 ms | Highest: 1,225 ms
Код:
for(new j = strlen(string); j != 0; j --)
{
// printf("%d", j - 1);
}
Re: Fastest String Loop -
SyS - 11.10.2016
Quote:
Originally Posted by SickAttack
for(new j = (strlen(string) - 1); j != 0; j --)
|
It wont reach @ first index.
Re: Fastest String Loop -
SickAttack - 11.10.2016
Quote:
Originally Posted by Sreyas
It wont reach @ first index.
|
Yeah, thanks for pointing that out. Anyhow, it still remains the fastest with changes:
// ** Lowest: 1,193 ms | Highest: 1,225 ms
Код:
for(new j = strlen(string); j != 0; j --)
{
// printf("%d", j - 1);
}
Re: Fastest String Loop -
Gammix - 11.10.2016
Sequence matters, i find the regular sequence starting from 0 is most used. So i consider the second last to be best and good thing most people use that already!
Re: Fastest String Loop -
Yashas - 11.10.2016
If amx files were compiled executables (exe, dll), then your argument would be wrong as the speed of the logical operations depends on the CPU.
I am not sure how things work for PAWN. You are probably right because the AMX machine has a lot of work (decoding the instruction) to do just before doing the actual CPU > or != instruction so the CPU implementation shouldn't really contribute. PAWN will always be very slow.
Re: Fastest String Loop -
SyS - 11.10.2016
hmm can you test its speed? im not home these days so i cant test myself.
Код:
new j=strlen(string);
do
{
j--;
//-----
}while(j!=0);
Re: Fastest String Loop -
SickAttack - 11.10.2016
Quote:
Originally Posted by Sreyas
hmm can you test its speed? im not home these days so i cant test myself.
Код:
new j=strlen(string);
do
{
j--;
//-----
}while(j!=0);
|
It's really slow, I added it to the main post.
-----------------
Well, I'm not sure which one is the best option now.
for(new j = (strlen(cmd) - 1); j > -1; j --)
for(new j = (strlen(cmd) - 1); j != 0; j --)
Both seem to be the fastest in a production scenario.
-----------------
for(new j = strlen(string); j != 0; j --)
It becomes very slow in a production use, but seems to be the fastest in a plain loop.
-----------------
for(new j = 0, k = strlen(string); j != k; j ++)
Slice's fastest loop to his experience seems slower than the first loop in a production use, but faster in a plain loop.
Re: Fastest String Loop -
Luicy. - 11.10.2016
Why are everyone upset with the speed? Like,
new something[3]; or new something0, something1, something2;
It just a few ms different, why do it fucking matter?
Re: Fastest String Loop -
SyS - 11.10.2016
Quote:
Originally Posted by SickAttack
It's really slow, I added it to the main post.
|
my assumptions sucks

@Luicy. it can be fun in other words and also very useful.How optimized is our code that much is good no? its not a must though.
Re: Fastest String Loop -
SickAttack - 11.10.2016
Quote:
Originally Posted by Luicy.
Why are everyone upset with the speed? Like,
new something[3]; or new something0, something1, something2;
It just a few ms different, why do it fucking matter?
|
"best coding practices"
If you're going to make a game, then you're going to do what it takes to make it cause the less stutter possible.
Re: Fastest String Loop -
Jayse - 11.10.2016
I could've tried to find a faster method, but the topic contains what I had on my mind. Also it's always nice to see more optimized ways to do something such as looping. That's the main reason why most of scripters use y_iterate instead of plain player loops.
Re: Fastest String Loop -
Stinged - 11.10.2016
How do you do your benchmarks?
I tried out the third
slowest one:
Код:
new j = -1;
while(string[++ j])
And this one (which is supposedly faster):
Код:
for (new j = strlen(str)-1; j > -1; j--)
(All according to your list)
It turned out that the while loop is actually faster.
(I tested it out on a 34 characters long string)
Either I'm doing something wrong, or you are.
Can you please post the code?
Re: Fastest String Loop -
Konstantinos - 11.10.2016
I ran few tests and if the length is long, these two made a difference:
This is the fastest:
pawn Код:
for(j = strlen(string); --j != -1;)
and second comes:
pawn Код:
for(j = strlen(string); j != 0; j --)
http://pastebin.com/raw/AdJuRncr
Results:
pawn Код:
Bench for 1st: executes, by average, 168.35 times/ms.
Bench for 2nd: executes, by average, 156.76 times/ms.
Bench for 3rd: executes, by average, 173.58 times/ms.
Bench for 4th: executes, by average, 168.80 times/ms.
Bench for 5th: executes, by average, 171.69 times/ms.
Bench for 6th: executes, by average, 149.65 times/ms.
Bench for 7th: executes, by average, 174.24 times/ms.
Bench for 8th: executes, by average, 223.66 times/ms.
Bench for 9th: executes, by average, 174.86 times/ms.
Bench for 10th: executes, by average, 174.98 times/ms.
Bench for 11th: executes, by average, 217.67 times/ms.
EDIT: I removed the part I was setting the text in the loops and did few more. The result for longer strings are the same but for 1 or 2 characters only, this becomes the fastest:
pawn Код:
for(j = 0; string[j]; j ++)