切换到Parallel.For(来自顺序)

时间:2014-07-08 00:19:13

标签: c# for-loop .net-4.0 parallel-processing

我正在更改我的代码以使用Parallel.For而不是简单的for语句。我的代码有3个嵌套for,可以逐个像素地对两个图像进行比较。

在最后for我保存了切割点(TopLeft和BottomRight),应该剪切图像以减少文件大小。

//For the 1st way:
int firstY = listBit[index].Height; //starts from the end.
int firstX = listBit[index].Width;
int lastY = 0; int lastX = 0;
//For the 2nd way:
var valueX = new List<int>();
var valueY = new List<int>();
//For the 3rd way:
var startY = new bool[listBit[index].Height];
var startX = new bool[listBit[index].Width];

//x - width - sides
Parallel.For(0, width, x =>
{
     //y - height - up/down
     for (int y = 0; y < height; y++)
     {
         //image1 and image2 are bitmaps (inside a help class).
         if (image1.GetPixel(x, y) == image2.GetPixel(x, y))
         {
             image2.SetPixel(x, y, transparent);
         }
         else
         {
             //Here i save the cut points, to crop the image later.

             //Old way: (compare and store in a variable)
             if (x < firstX)
             {
                 firstX = x;
             } //here was the rest of it, works only with a sequencial loop.

             //2nd way: (store in a list all entries, the List<> will blow, eventually)
             valueX.Add(x);
             valueY.Add(y); //and later use LINQ to get the values (first and last).

             //3rd way: (store a flag for each pixel, works)
             startX[x] = true;
             startY[y] = true;
             //but how can I get the first and last values==true of this array?

             //Any other idea?
         }
     }
 });

获得切入点的更好方法是什么?

0 个答案:

没有答案