Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: Array parsing problems

[ Edited ]

you can't be serious is asying that there is no reason to.

 

If i needed to do a certain operation on every element in an array, its a hell of a lot easyier to set up an iterator to go through each element automatically rather than refer to each element individually. 

 

this is also a good idea because  hard code the specific elements is going to run into trouble if your aray comes back with elements that do not meet your hard coded expectations.

 

 

 

 

 

- - - - - - - - - -
Developer
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: Array parsing problems

 


JRab wrote:

ah ok, sorry about that. in that case, change the for loop to this:

 

 

for (var n:int = 0; n < this.setsArray.length; n++)
{

     (... your function stuff)

}

 


Not to be a stickler... but this should be better optimized as:

 

for (var n:int = 0, l:int = this.setsArray.length; n < l; n++)
Developer
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: Array parsing problems

 


dtater wrote:
or, if i needed to do a certain operation on each element in an array. like this: for each index in this array, please do this. ^^^^ sounds like a *perfect* reason to use a for each loop.

No, thats what a for loop is for. It's _way_ faster, and will actually go in the proper order.

 

Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: Array parsing problems

Why do you need to do this?  i thought the length property of an array *already* returned an int.

 

if that was the case, then there would be no reason to make a new variable cast as an int.

 

 

- - - - - - - - - -
Developer
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: Array parsing problems

[ Edited ]

 


dtater wrote:

Why do you need to do this?  i thought the length property of an array *already* returned an int.

 

if that was the case, then there would be no reason to make a new variable cast as an int.

 

 


 

It's much faster because you do not need to look up .length each time, which is actually a function call. You won't notice it on small loops, but run a benchmark on 100,000 items and you'll see a substantial speed increase.

 

In general, it's just a good habit to get into, you should never lookup length everytime you iterate.

 

[Edit] Just ran a quick test on 100k items, when lookin up length each time it takes 12ms, when storing it off, it takes only 2ms.

Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: Array parsing problems

[ Edited ]

If the for each structure *does* look up the length each time, then yeah, i agree.  doing the loop manually would be a much faster way.

 

 

My understanging was that the length property did not need to be accessed each time.

 

for each (X in Y)

 

was functionally identical to

 

 

tmp_length = array.length;

for (int i = 0 ; i < tmp_length; i++) {

//code

}

 

but instead of having to do the logic of the loop your self, it was automacially done

 


EDIT: small typo

 

- - - - - - - - - -
Developer
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: Array parsing problems

[ Edited ]

 


dtater wrote:

If the for each structure *does* look up the length each time, then yeah, i agree.  doing the loop manually would be a much faster way.

 


 

For each's are just inherintly slow, you only ever use them when there is absolutely no other way. I'm not sure what exactly they do for an array... they probably don't look up length at all, I dunno it's such a wierd thing to be doin

 

Here's benchmarks for all 3 methods, looping 100k items:

For loop, with a stored length: 2ms
For loop, looking up length each time: 12ms
For each: 42ms

Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: Array parsing problems

 


shawnblais wrote:

 

[Edit] Just ran a quick test on 100k items, when lookin up length each time it takes 12ms, when storing it off, it takes only 2ms.


 

Oh wow.  Ok, i'll go back and make the change.  at the moment, my array is only 30 elements, but i am designing this whole application do deal with many mutiples of that.

 

 

I was not aware that Actionscript looked up the length each time.  the languages i am used to working with do not; they chache it automatically for the duration of the loop.

 

thanks for your input, guys/girls!

- - - - - - - - - -
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Array parsing problems

 


shawnblais wrote:

 


JRab wrote:

ah ok, sorry about that. in that case, change the for loop to this:

 

 

for (var n:int = 0; n < this.setsArray.length; n++)
{

     (... your function stuff)

}

 


Not to be a stickler... but this should be better optimized as:

 

for (var n:int = 0, l:int = this.setsArray.length; n < l; n++)

 

thanks for the heads up shawn. i agree it would make the for loop less error prone but its subjective on the note of optimization. and actually if i had to re-write it, it'd be:

 

 

var arrayLength:int = this.setsArray.length;

for (var n:int = 0; n < arrayLength; n++)

(...)

 

it not only "optimizes" but it also makes it more readable dontcha think Smiley Wink

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: Array parsing problems

Ya, thats how I always used to do it, but nowadays I really like the compactness of just doing the whole thing in one line. Either way though, it's all good Smiley Happy