Raised This Month: $12 Target: $400
 3% 

Solved Loop headers


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Vieni
Member
Join Date: Apr 2018
Old 06-09-2020 , 17:55   Loop headers
Reply With Quote #1

The question would be that, which option uses less cpu:
HTML Code:
1. for(new i; i < ArraySize(dynamicArray); i++)

OR

2. 
new lArraySize = ArraySize(dynamicArray)
for(new i; i < lArraySize; i++)
So basically I'm asking if the functions in the header called everytime it's being looped

Last edited by Vieni; 06-09-2020 at 19:02.
Vieni is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-09-2020 , 18:03   Re: Loop headers
Reply With Quote #2

The second one. You want to aim at having the least number of native calls. Storing the value in a variable will result in ArraySize() being called only once, instead of ArraySize number of times.
__________________
Bugsy is offline
Vieni
Member
Join Date: Apr 2018
Old 06-09-2020 , 18:57   Re: Loop headers
Reply With Quote #3

And why is that if I create a for loop like this
HTML Code:
for(new i; statement; i++)
the i variable ain't restarted at every loop creating a possible infinite loop?

Then does this whole stuff look something like this?
HTML Code:
for(<done at start>; <checked every loop>; <done every loop>)

Last edited by Vieni; 06-09-2020 at 18:57.
Vieni is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-09-2020 , 18:59   Re: Loop headers
Reply With Quote #4

You answered your own question. The first part is done when the loop is initiated.

Here's another way to look at it.
PHP Code:
new i;

for ( 
15 <= 20 i++ )

     
//15 , 16, 17, 18, 19, 20

__________________
Bugsy is offline
Vieni
Member
Join Date: Apr 2018
Old 06-09-2020 , 19:01   Re: Loop headers
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
You answered your own question. The first part is done when the loop is initiated.

Here's another way to look at it.
PHP Code:
new i;

for ( 
15 <= 20 i++ )

     
//15 , 16, 17, 18, 19, 20

Well, thx, appreciate it!
Vieni is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-10-2020 , 03:12   Re: Loop headers
Reply With Quote #6

To add to the first question:
If you used sizeof then for(...; i < sizeof; i++) would be fine since regular array sizes are known at compile time.
__________________

Last edited by HamletEagle; 06-10-2020 at 03:13.
HamletEagle is offline
pattytaylor
New Member
Join Date: Jun 2020
Old 06-29-2020 , 23:07   Re: Loop headers
Reply With Quote #7

Thx, appreciate it!

Last edited by pattytaylor; 07-14-2020 at 05:49.
pattytaylor is offline
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 08:14.


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