× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Asher613Smith@xxxxxxx wrote:
If you take a look at the generated machine code
for a sort and for the for loop, you will only
see part of the savings for doing a sort. The BIG saving is not having go do a test on every
element in the array <<SNIP loop version>>
as array(1) will always be the min and
array(elementCount) will always be the max after the sort.

Maybe I am clueless, but how can a sort of an array not examine and compare for every element just like the loop will? And then the sort may even [re]write each element? In the given loop example, no change is ever made to any of the elements, although it must examine & compare each element. Yet, it only writes to variable /max/ when the element has the larger value.

What /generated machine code/ is noted? The MI, or the RISC instructions? I would not expect the RISC listing to be any smaller than the coded loop. The MI is of course moot, since the actual instructions being run are the RISC. The number one rule of performance is broken by use of a sort; i.e. the rule is: move no data, or as minimal as possible. The coded loop which only uses /max/ is almost guaranteed to be optimized for its static location; i.e. data movement is limited, but when it is not, movement of data is always into the same location [unlike with an array].

I will concede the sort is /elegant/ [for its result of ordered values and as a BIF] and may even appear so in an MI listing, but I still do not see how it is more efficient. And FWiW I would actually avoid a sort unless there was later code which actually required the sorted array as input. In review of the code, it would seem odd to find a sort on an array solely to get a min & max; i.e. one would hope the comments would be very clear not only to state the purpose of the sort, but to call out that any expectation of the integrity of the original sequence of data in the array is henceforth lost. Restated: the array was not sorted already, else the max\min would already be known, so adding the sort may be problematic for the array originally being unordered and thus code expecting that.

Regards, Chuck

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.