# question on Max and Min

Discussion in 'Mathematica' started by Arkadiusz.Majka, Sep 6, 2007.

Hi all,
Please take o while and think if

Max[longList]-Min[longList]

is the fastest way of calculating the difference of maximal and
minimal values of (very) long list.

Arek

2. ### Jean-Marc GullietGuest

I would say yea for a one-dimensional list made of homogeneous atomic
elements (i.e. all element have the same basis type such as machine-size
floating-point numbers or integers).

The following example tests a vector of 100 million floating-point
numbers (occupying about 800 Mo in RAM) in less than 0.9 second. To me,
I would say the list is very large and the test fast enough. Of course,
you may have a different opinion. Also, sorting the list before talking
the first and last elements is much slower even when using Ordering. (I
believe that Max and Min have been optimized to work fast on large list.)

In[1]:= longList = RandomReal[{-10000, 10000}, {10^8}];
ByteCount[longList]
Max[longList] - Min[longList] // Timing

Out[2]= 800000076

Out[3]= {0.875, 20000.}

Jean-Marc Gulliet, Sep 7, 2007