SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 11/10/2016, 03:03 AM   #1
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,778
Reputation: 754
Default Fastest String Loop

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

Code:
new j = (strlen(string) - 1);
while(j != -1)
{
	j --;
}
// ** Lowest: 2,648 ms | Highest: 2,700 ms

Code:
new j, k = strlen(string);
while(j != k)
{
	j ++;
}
// ** Lowest: 2,619 ms | Highest: 2,686 ms

Code:
new j = -1;
while(string[++ j])
// ** Lowest: 2,536 ms | Highest: 2,599 ms

Code:
new j = strlen(string);
do
{
	j --;
}
while(j != 0);
// ** Lowest: 1,428 ms | Highest: 1,450 ms

Code:
for(j = 0; string[j]; j ++)
// ** Lowest: 1,422 ms | Highest: 1,440 ms

Code:
for(new j = 0; string[j] != EOS; j ++)
// ** Lowest: 1,320 ms | Highest: 1,351 ms

Code:
for(new j = (strlen(string) - 1); j > -1; j --)
// ** Lowest: 1,314 ms | Highest: 1,349 ms

Code:
for(new j = strlen(string); --j != -1;)
// ** Lowest: 1,309 ms | Highest: 1,319 ms

Code:
for(new j = 0, k = strlen(string); j < k; j ++)
// ** Lowest: 1,299 ms | Highest: 1,324 ms

Code:
for(new j = 0, k = strlen(string); j != k; j ++)
// ** Lowest: 1,193 ms | Highest: 1,225 ms

Code:
for(new j = strlen(string); j != 0; j --)
{
	// printf("%d", j - 1);
}

Last edited by SickAttack; 11/10/2016 at 02:54 PM.
SickAttack is offline   Reply With Quote
Old 11/10/2016, 03:48 AM   #2
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,950
Reputation: 500
Default Re: Fastest String Loop

Quote:
Originally Posted by SickAttack View Post
for(new j = (strlen(string) - 1); j != 0; j --)
It wont reach @ first index.
SyS is offline   Reply With Quote
Old 11/10/2016, 04:57 AM   #3
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,778
Reputation: 754
Default Re: Fastest String Loop

Quote:
Originally Posted by Sreyas View Post
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

Code:
for(new j = strlen(string); j != 0; j --)
{
	// printf("%d", j - 1);
}
SickAttack is offline   Reply With Quote
Old 11/10/2016, 05:55 AM   #4
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Canada
Posts: 1,761
Reputation: 840
Default Re: Fastest String Loop

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!
__________________

Follow me on Github: Follow me on Spotify: Donate on Paypal:

Gammix is offline   Reply With Quote
Old 11/10/2016, 07:09 AM   #5
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 882
Reputation: 315
Default Re: Fastest String Loop

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.
Yashas is offline   Reply With Quote
Old 11/10/2016, 07:54 AM   #6
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,950
Reputation: 500
Default Re: Fastest String Loop

hmm can you test its speed? im not home these days so i cant test myself.

Code:
new j=strlen(string);

do
{
  j--;
 //-----
}while(j!=0);
SyS is offline   Reply With Quote
Old 11/10/2016, 02:56 PM   #7
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,778
Reputation: 754
Default Re: Fastest String Loop

Quote:
Originally Posted by Sreyas View Post
hmm can you test its speed? im not home these days so i cant test myself.

Code:
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.
SickAttack is offline   Reply With Quote
Old 11/10/2016, 03:16 PM   #8
Luicy.
Banned
 
Join Date: Dec 2014
Location: Sweden, Skövde
Posts: 776
Reputation: 79
Default Re: Fastest String Loop

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?
Luicy. is offline   Reply With Quote
Old 11/10/2016, 03:18 PM   #9
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,950
Reputation: 500
Default Re: Fastest String Loop

Quote:
Originally Posted by SickAttack View Post
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.
SyS is offline   Reply With Quote
Old 11/10/2016, 03:23 PM   #10
AndySedeyn
High-roller
 
AndySedeyn's Avatar
 
Join Date: Feb 2014
Location: Belgium
Posts: 1,207
Reputation: 681
Default Re: Fastest String Loop

- Nvm.
AndySedeyn is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String vs loop cdoubleoper Scripting Help 7 02/05/2016 09:17 PM
[Tutorial] How to make the fastest plain player-loop JM_Millers Tutorials 16 05/09/2014 07:55 AM
Which one is fastest loop? AA9 Scripting Help 9 10/02/2014 06:35 PM
Fastest way to reset Enum with loop not working Swimor Scripting Help 9 23/05/2013 05:47 AM
The fastest loop Tomer!.$ Scripting Help 23 25/04/2012 08:30 PM


All times are GMT. The time now is 05:23 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.