[algorithm] What is the best way to get the minimum or maximum value from an Array of numbers?

Math.max() is actually as3 code compiled to AVM2 opcodes, and as such is not more "native" than any other as3 code. As a consequence, it is not necessarily the fastest implementation.

Actually, given that it works on Array type, it is slower than carefully written code usign Vector:

I did a quick benchmark comparison of several naive Vector and Array implementations of Math.max, using gskinner's PerformanceTest (Vector and Array being filled with identical random Numbers). The fastest Vector implementation appeared to be more than 3x faster than Math.max with recent AIR SDK/release player (flash player WIN 14,0,0,122 RELEASE, compiled with AIR SDK 14):

average 3.5 ms for 1,000,000 values, compared to Math.max() average of 11ms :

function max(values:Vector.<Number>):Number
{
    var max:Number = Number.MIN_VALUE;
    var length:uint = values.length;
    for (var i:uint = 0; i < length ; ++i)
        if (values[i] > max)
            max = values[i];
    return max;
}

Conclusion is that if you are concerned by performance, you should use Vector over Array anywhere you can in the first place, and not always rely on default implementations, especially when they force the use of Array

PS:same implementation with a for each() loop is 12x slower ...!

Examples related to algorithm

How can I tell if an algorithm is efficient? Find the smallest positive integer that does not occur in a given sequence Efficiently getting all divisors of a given number Peak signal detection in realtime timeseries data What is the optimal algorithm for the game 2048? How can I sort a std::map first by value, then by key? Finding square root without using sqrt function? Fastest way to flatten / un-flatten nested JSON objects Mergesort with Python Find common substring between two strings

Examples related to actionscript-3

How can I convert this one line of ActionScript to C#? Difference between e.target and e.currentTarget What is the best way to get the minimum or maximum value from an Array of numbers?

Examples related to apache-flex

Failed to load JavaHL Library How to pass "Null" (a real surname!) to a SOAP web service in ActionScript 3 How do I get a HttpServletRequest in my spring beans? What is the best way to get the minimum or maximum value from an Array of numbers? Eclipse memory settings when getting "Java Heap Space" and "Out of Memory"

Examples related to actionscript

How to detect when a youtube video finishes playing? How to pass "Null" (a real surname!) to a SOAP web service in ActionScript 3 What is the best way to get the minimum or maximum value from an Array of numbers?

Examples related to complexity-theory

Differences between time complexity and space complexity? Determining complexity for recursive functions (Big O notation) How to find time complexity of an algorithm How can building a heap be O(n) time complexity? HashMap get/put complexity Big-oh vs big-theta Is log(n!) = T(n·log(n))? Time complexity of accessing a Python dict What are the differences between NP, NP-Complete and NP-Hard? What's the fastest algorithm for sorting a linked list?