title: Is IEnumerable.Max() the fastest way?
body: <p>I am developing a part of a software, where I have a list (<code>List&lt;Sample&gt;</code> at the moment) of samples like the following:</p>
  
  <pre><code>public class Sample
  {
      //...
      public double ValueChannel1 { get; set; }
      public double ValueChannel2 { get; set; }
      //...
  }
  </code></pre>
  
  <p>These lists have between ~100 and several thousands of these samples and there are around 100k samples per second.<br>
  Now I need to find the maximum and minimum value out of each of these lists, which I do the following way at the moment:</p>
  
  <pre><code>var Ch1Max = task.Samples.Max&lt;Sample&gt;(s =&gt; s.ValueChannel1);
  var Ch1Min = task.Samples.Min&lt;Sample&gt;(s =&gt; s.ValueChannel1);
  var Ch2Max = task.Samples.Max&lt;Sample&gt;(s =&gt; s.ValueChannel2);
  var Ch2Min = task.Samples.Min&lt;Sample&gt;(s =&gt; s.ValueChannel2);
  </code></pre>
  
  <p>To no surprise this is not very fast, so I was asking myself if there is something faster to do that, but I could not think of or find one?</p>
  
  <p><strong>Does someone know a faster way to do this?</strong>
  Maybe there is a way to find min and max with "one loop" instead of one for min and one for max?</p>
  
  <p><strong>Edit:</strong><br>
  I profiled the current code with the following results:<br>
  731 tasks each containing one of these lists needed 845ms to process and 95% of that where the min/max searching.<br>
  I have no specific "target time", but as this runs all the time in my app (as it is capturing measurement data) it should cause as little CPU utlization as possible, to keep the hardware requirements as low as possible...</p>
  
  <p><strong>Best solution found:</strong><br>
  In the end I choose the soltuion from Tim, as it was even a little faster then Konrad ones:<br>
  Tim's solution caused a speed-up of ~53% and Konrads's "only" ~43%.</p>
  
  <p><strong>Final solution (for now):</strong></p>
  
  <pre><code>double Ch1Max = Double.MinValue, Ch1Min = Double.MaxValue;
  double Ch2Max = Double.MinValue, Ch2Min = Double.MaxValue;
  
  var samples = task.Samples.ToArray();
  int count = samples.Length;
  for (int i = 0; i &lt; count; ++i)
  {
      var valueChannel1 = samples[i].ValueChannel1; // get only once =&gt; faster
      if (valueChannel1 &gt; Ch1Max) Ch1Max = valueChannel1;
      if (valueChannel1 &lt; Ch1Min) Ch1Min = valueChannel1;
  
      var valueChannel2 = samples[i].ValueChannel2;
      if (valueChannel2 &gt; Ch2Max) Ch2Max = valueChannel2;
      if (valueChannel2 &lt; Ch2Min) Ch2Min = valueChannel2;
  }
  </code></pre>
  
  <p>This sums up to a speed up of ~70% compared to my initial solution...</p>