AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimizing Plugins: For Loop Comparisons (https://forums.alliedmods.net/showthread.php?t=113974)

Dygear 12-31-2009 19:26

Optimizing Plugins: For Loop Comparisons
 
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

fysiks 12-31-2009 20:09

Re: Optimizing Plugins: For Loop Comparisons
 
Quote:

Originally Posted by Dygear (Post 1038479)
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.

Dygear 12-31-2009 20:49

Re: Optimizing Plugins: For Loop Comparisons
 
Quote:

Originally Posted by fysiks (Post 1038521)
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 (Post 1038521)
Alos, this forum is not for asking questions. Ask in scripting help.

Sorry.

fysiks 01-01-2010 01:00

Re: Optimizing Plugins: For Loop Comparisons
 
Quote:

Originally Posted by Dygear (Post 1038540)
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.

Dygear 01-01-2010 03:34

Re: Optimizing Plugins: For Loop Comparisons
 
Quote:

Originally Posted by fysiks (Post 1038663)
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.

Greenberet 01-01-2010 04:57

Re: Optimizing Plugins: For Loop Comparisons
 
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

fysiks 01-01-2010 18:14

Re: Optimizing Plugins: For Loop Comparisons
 
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.

Dygear 01-02-2010 01:57

Re: Optimizing Plugins: For Loop Comparisons
 
Quote:

Originally Posted by Greenberet (Post 1038721)
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 (Post 1039423)
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.


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

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.