Calculate temperature based on differing regression coefficients The following function calculates temperature based off of a given suction pressure, where different regression coefficients are assigned depending on the value of the pressure.

The regression equation is fairly standard, of the form:

$$ax^2 + bx + c$$

where \$x\$ is the pressure, and \$a\$, \$b\$, and \$c\$ are the coefficients. There are three different set of coefficients that are outlined in the code. I apply the regression equation by mapping the current pressure to the coefficients with LINQ.

I would like to optimize this function, as it takes in as a parameter a very large array of pressures, and needs to perform this calculation on every element in the array, and return those numbers as either an array or a list.

public static List&lt;double&gt; GetSSTList(double[] suctionPressureList)
{
    var result = new List&lt;double&gt;();
    var coefficientList = new List&lt;double[]&gt;() {
        new double[] { -28.43, 1.96582, -0.1582 },
        new double[] { -27.955, 2.4866, -0.055 },
        new double[] { -17.178, 0.5952, -0.0048 }
    };

    foreach (var psig in suctionPressureList)
    {
        double[] coefficients;

        if (psig &gt;= -9 &amp;&amp; psig &lt;= -4)
        {
            coefficients = coefficientList[0];
        }
        else if (psig &gt; -4 &amp;&amp; psig &lt;= 9)
        {
            coefficients = coefficientList[1];
        }
        else if (psig &gt; 9 &amp;&amp; psig &lt;= 85)
        {
            coefficients = coefficientList[2];
        }
        else
        {
            coefficients = null;
        }

        var sst = coefficients.Select(c =&gt; c * Math.Pow(psig, Array.IndexOf(coefficients, c))).Sum();
        result.Add(sst);
    }

    return result;
}
There are a couple of things you can do to speed this up.


Set the initial capacity of result


The List&lt;T&gt;() constructor uses a default initial capacity. When the number of elements in the list goes beyond its capacity, the list must be resized. We can avoid resizing the list by setting the initial capacity of result to suctionPressureList.Length.


Remove the call to Array.IndexOf.


Assuming that each element in coefficients is unique, we can remove the call to Array.IndexOf. We can do this by either using the overload of Select that gives us the index of the element:

var sst = coefficients.Select((c, i) =&gt; c * Math.Pow(psig, i)).Sum();


or we can manually do the summation:

double sst = 0;
for (var i = 0; i &lt; coefficients.Length; i++)
{
    sst += coefficients[i] * Math.Pow(psig, i);
}


With these changes, these are the results that I got for an input array of size 1,000,000:



BenchmarkDotNet-Dev=v0.9.3.0+
OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Core(TM) i7-6650U CPU @ 2.20GHz, ProcessorCount=4
Frequency=2156248 ticks, Resolution=463.7685 ns
HostCLR=MS.NET 4.0.30319.42000, Arch=64-bit RELEASE [RyuJIT]

Type=Foo  Mode=Throughput

   Method |      Median |    StdDev |
--------- |------------ |---------- |
      New |  80.4571 ms | 2.6079 ms |
 Original | 220.0254 ms | 9.9705 ms |