Wow, good thread.

Here's my 2 cents. Perhaps one other thing to factor is if the array is
sorted:

"If the array is not sorted the best you can do is iterate through all of
the items in the array, checking each element, until you either find the
matching string or reach the end of the array. This approach is said to run
in linear time since the amount of time required for the algorithm to run is
linearly proportional to the length of the array. That is, if the array
contains 1,000 elements, you will have to check at most 1,000 elements
(assuming the element is in the last position of the array, or is not found
at all in the array); if the array contains 1,000,000 elements, you will
have to check at most 1,000,000 elements. Hence, there is a linear
proportionality between the amount of work that needs to be done to search
the array and the size of the array (namely, a proportion of 1).

However, if your array is sorted there is a much faster algorithm you can
employ to determine whether or not a particular element exists within the
array, known as the binary search algorithm. In this article we will examine
how this algorithm works, its running time, and how to use the
Array.BinarySearch method, which searches a sorted array using the binary
search algorithm."



Src: http://aspnet.4guysfromrolla.com/articles/110602-1.aspx
Src: http://code.dreamincode.net/snippet515.htm

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Tyler
Wright
Sent: Tuesday, September 19, 2006 4:36 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Find item in array


:)

So, for the sake of another one of those big arguments over what's better
where no one ever test, I wrote some code to find out exact results:

The following code executes one particular test on my Windows XP machine.
For results I got an averate of 347 miliseconds executeing the --i -(-1) and
343 ms with the for..in loop. I was surprised at how close they were. Now
that being the difference over 10,000 iterations, I can't really say for..in
is much faster, if at all. Perhaps the results are different on someone
else's machine, or perhaps different results from a different type of array.
Now iterating through the entire array the for..in did seem to be faster,
but for the test I chose a value directly in the middle so each method had
the same amount of value's to cover. anyway, I conclude nothing. ;)

except that we should always test our results.

Tyler

var myArray:Array = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p"];

var time:Number = getTimer();
var loop:Number = 9999;
while (loop--)
{
    /*/
    var j:Number = myArray.length
    while (--j -(-1)) {
        if(myArray[j] == "h"){
            break;
        }
    }
    /*/
    for (var j in myArray)
    {
        if (myArray[j] == "h")
            break;
    }
    //*/
}
trace(getTimer() - time);

// 347
// 343

On 9/19/06, Merrill, Jason <[EMAIL PROTECTED]> wrote:
>
> >>>>using a for..i..in loop will always be faster. Even more then --a
> -(-1)
>
> I'm ducking and running for cover.
>
> Jason Merrill
> Bank of America
> Learning & Organization Effectiveness - Technology Solutions
>
>
>
>
>
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com



_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to