Should I stick with LINQ in this simple case?

Consider:

            float[] xPos = new float[pt3f.Count];
            float[] yPos = new float[pt3f.Count];
            float[] zPos = new float[pt3f.Count];
            for (int i = 0; i < pt3f.Count; i++)
            {
                xPos[i] = pt3f[i].X;
                yPos[i] = pt3f[i].Y;
                zPos[i] = pt3f[i].Z;
            }

I know that I can use LINQ here

                var xPos = pt3f.Select(I => I.X).ToArray();
                var yPos = pt3f.Select(I => I.Y).ToArray();
                var zPos = pt3f.Select(I => I.Z).ToArray();

so my questions are different from cleaner code using LINQ, are there any performance benefits?

I think in terms of performance, using one for-loop is faster, I'm right, i.e. 3 Linqs will eventually be converted to 3 cycles.

+4
source share
1 answer

Yes, the LINQ example will result in three loops instead of one in the first example. But in most practical cases, you will not notice any difference, at least on small arrays.

If you really didn't notice a performance issue, I personally would prefer a more readable version of LINQ.

+3
source

Source: https://habr.com/ru/post/1629363/


All Articles