Raised This Month: $2 Target: $400
 0% 

Optimizing Plugins: For Loop Comparisons


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dygear
SourceMod Donor
Join Date: Apr 2004
Location: Levittown, NY
Old 12-31-2009 , 19:26   Optimizing Plugins: For Loop Comparisons
Reply With Quote #1

Optimizing Plugins (AMX Mod X Scripting): For Loop Comparisons

Code:
new string[256] = "something long" for (new i=0; i<strlen(string); i++)    //...code

Agreed that is bad, but this:

Code:
new string[256] = "something long" new len = strlen(string) for (new i=0; i<len; i++)    //...code

Why not:

Code:
new string[256] = "something long" for (new i = 0, len = strlen(string); i < len; i++)    //...code
__________________

Last edited by Dygear; 12-31-2009 at 19:31.
Dygear is offline
Send a message via AIM to Dygear Send a message via MSN to Dygear Send a message via Skype™ to Dygear
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-31-2009 , 20:09   Re: Optimizing Plugins: For Loop Comparisons
Reply With Quote #2

Quote:
Originally Posted by Dygear View Post
Why not:

Code:
new string[256] = "something long" for (new i = 0, len = strlen(string); i < len; i++)    //...code
It calls strlen() native every iteration. Same as the first code you posted. Bad.

Alos, this forum is not for asking questions. Ask in scripting help.
__________________
fysiks is offline
Dygear
SourceMod Donor
Join Date: Apr 2004
Location: Levittown, NY
Old 12-31-2009 , 20:49   Re: Optimizing Plugins: For Loop Comparisons
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
It calls strlen() native every iteration. Same as the first code you posted. Bad.
Isn't it the same as doing this?

Code:
new string[256] = "something long, that's what she said."; new i = 0, len = strlen(string); for (; i < len; i++)     //...code

Just wondering if it's different in PAWN then the other languages I use.

Quote:
Originally Posted by fysiks View Post
Alos, this forum is not for asking questions. Ask in scripting help.
Sorry.
__________________
Dygear is offline
Send a message via AIM to Dygear Send a message via MSN to Dygear Send a message via Skype™ to Dygear
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-01-2010 , 01:00   Re: Optimizing Plugins: For Loop Comparisons
Reply With Quote #4

Quote:
Originally Posted by Dygear View Post
Isn't it the same as doing this?

Code:
new string[256] = "something long, that's what she said."; new i = 0, len = strlen(string); for (; i < len; i++)     //...code

Just wondering if it's different in PAWN then the other languages I use.
No, it's not the same. strlen() in that code is not inside the for() so it is not called every iteration. When strlen() is inside the for() the it is called which is slower than just accessing a value from a variable.
__________________
fysiks is offline
Dygear
SourceMod Donor
Join Date: Apr 2004
Location: Levittown, NY
Old 01-01-2010 , 03:34   Re: Optimizing Plugins: For Loop Comparisons
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
When strlen() is inside the for() the it is called which is slower than just accessing a value from a variable.
Ok, because I was under the impression that the first statement in the for loop is the initialization line.

Code:
for (initialization; condition; step)     // Code

Code:
for (i = 1; i <= 10; i++)     printf("%d", i);

If what your saying is true, then this will never reach 10, it will just keep on printing out 1s for the rest of time. Does not make much sense to me.
__________________

Last edited by Dygear; 01-01-2010 at 03:36.
Dygear is offline
Send a message via AIM to Dygear Send a message via MSN to Dygear Send a message via Skype™ to Dygear
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 01-01-2010 , 04:57   Re: Optimizing Plugins: For Loop Comparisons
Reply With Quote #6

dygear you are right.
it's more or less the same.
The difference is just the namespace.
Code:
new string[256] = "something long, that's what she said."; new len = strlen(string); for (new i = 0; i < len; i++) { // len is visible here } // len is still visible here

Code:
new string[256] = "something long, that's what she said."; for (new i = 0, len = strlen(string); i < len; i++) { // len is visible } // len isn't visible here
__________________
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-01-2010 , 18:14   Re: Optimizing Plugins: For Loop Comparisons
Reply With Quote #7

Oh, I didn't see that it was a comma separating new i = 0 and len declaration. I thought it was still part of the condition. Sorry.
__________________
fysiks is offline
Dygear
SourceMod Donor
Join Date: Apr 2004
Location: Levittown, NY
Old 01-02-2010 , 01:57   Re: Optimizing Plugins: For Loop Comparisons
Reply With Quote #8

Quote:
Originally Posted by Greenberet View Post
Code:
new string[256] = "something long, that's what she said."; for (new i = 0, len = strlen(string); i < len; i++) { // len is visible } // len isn't visible here
That's interesting, I never really thought about visibility, but I've also never tripped over it too.

Quote:
Originally Posted by fysiks View Post
Oh, I didn't see that it was a comma separating new i = 0 and len declaration. I thought it was still part of the condition. Sorry.
Happens to all of us.
__________________
Dygear is offline
Send a message via AIM to Dygear Send a message via MSN to Dygear Send a message via Skype™ to Dygear
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

Forum Jump


All times are GMT -4. The time now is 10:10.


Powered by vBulletin®
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Theme made by Freecode