Proper way to process the huge amount of data from a sensor using Parallel.For I am getting the huge amount of data from a sensor. In order to speed up data processing, I thought of using Parallel.For. Below is the sample code:

object sync = new object();
private Random random = new Random();
private List&lt;byte&gt; container = new List&lt;byte&gt;();
private readonly int FRAME_WIDTH = 1000;
private readonly int DATA_LENGTH = 217088;
private readonly int FRAME_HEIGHT = 800;

private void getPointsFromSensor(out Point2D[] points2d, out Point3D[] points3d)
{
    points2d = new Point2D[DATA_LENGTH];
    points3d = new Point3D[DATA_LENGTH];
    for (var index = 0; index &lt; DATA_LENGTH; index++)
    {
        points2d[index] = new Point2D(random.Next(-index, index), random.Next(-index, index));
        points3d[index] = new Point3D(random.NextDouble(), random.NextDouble(), random.NextDouble());
    }
}

private void OnDataArrived(object sendor, DataArrivedEventArgs e)
{
    getPointsFromSensor(out Point2D[] points2d, out Point3D[] points3d);

    int validDataCount = 0;

    container.Clear(); // Remove old points
    // reserving 4 bytes for storing 'validDataCount' and we are going to modify it later
    container.AddRange(BitConverter.GetBytes(validDataCount));

    Parallel.For(0, DATA_LENGTH, index =&gt;
    {
        int points2dX = points2d[index].X;
        int points2dY = points2d[index].Y;

        if (points2dX &gt;= 0 &amp;&amp; points2dX &lt; FRAME_WIDTH &amp;&amp; points2dY &gt;= 0 &amp;&amp; points2dY &lt; FRAME_HEIGHT)
        {
            lock (sync)
            {
                container.AddRange(BitConverter.GetBytes(points3d[index].X));
                container.AddRange(BitConverter.GetBytes(points3d[index].Y));
                container.AddRange(BitConverter.GetBytes(points3d[index].Z));
            }
            Interlocked.Increment(ref validDataCount);
        }
    });

    var validDataCountBytes = BitConverter.GetBytes(validDataCount);//4 bytes
    container[0] = validDataCountBytes[0]; container[1] = validDataCountBytes[1];
    container[2] = validDataCountBytes[2]; container[3] = validDataCountBytes[3];
}


The Point2D and Point3D are defined as follows:

class Point2D
{
    public int X;
    public int Y;

    public Point2D(int x, int y)
    {
        X = x;
        Y = y;
    }
}

class Point3D
{
    public double X;
    public double Y;
    public double Z;

    public Point3D(double x, double y, double z)
    {
        X = x;
        Y = y;
        Z = z;
    }
}


It turns out that the Parallel.For version is taking much time compared to simple for loop.

I am wondering if there exists is a better way to use Parallel.For inside a callback function in C#.
Usually it makes absolutely no sense to use a parallel loop with a lock inside it. The performance loss you are talking about comes most probably from it. They cannot run in parallel because they have to wait for each other. The lock also costs some time and if it happens to often, the parallel loop might actually be slower then the normal one.

You might gain some performance if you redesign it in a way that does not requrie synchronization.