1

我想加快我的代码速度。我希望该片段能够更快地工作。我试图创建自己Parallel.For的循环实现,但失败了。没有错误,但tifDS似乎是空的:/ any1 可以帮助我吗?提前致谢!

原始循环:

for (int iY = 0; iY < srcDS.RasterYSize; iY++)
{
    byte[] srcData = new byte[srcDS.RasterXSize * 1];
    srcBand.ReadRaster(0, iY, srcDS.RasterXSize, 1, srcData, srcDS.RasterXSize, 1, 0, 0);

    for (int iBand = 0; iBand < _outBands; iBand++)
    {
        int[] bandLookup = lookup[iBand];

        int[] dstData = new int[srcData.Count()];
        for (int index = 0; index < srcData.Count(); index++)
        {
            byte b = srcData[index];
            dstData[index] = bandLookup[b];
        }

        tifDS.GetRasterBand(iBand + 1).WriteRaster(0, iY, srcDS.RasterXSize, 1, dstData,
                                                   srcDS.RasterXSize, 1, 0, 0);
    }
}

我的尝试:

for (int iY = 0; iY < srcDS.RasterYSize; iY++)
{
    byte[] srcData = new byte[srcDS.RasterXSize * 1];
    srcBand.ReadRaster(0, iY, srcDS.RasterXSize, 1, srcData, srcDS.RasterXSize, 1, 0, 0);

    Parallel.For<object>((long) 0, _outBands, () => 0,
                         (iBand, state, subtotal) =>
                             {
                                 int[] bandLookup = lookup[iBand];

                                 int[] dstData = new int[srcData.Count()];
                                 for (int index = 0; index < srcData.Count(); index++)
                                 {
                                     byte b = srcData[index];
                                     dstData[index] = bandLookup[b];
                                 }

                                 return new object[] {iBand, dstData};
                             },
                         o =>
                             {
                                 int iBand = (int) (o as object[])[0];
                                 int[] dstData = (o as object[])[1] as int[];
                                 lock (_getRasterBandLock)
                                 {
                                     tifDS.GetRasterBand(iBand + 1).WriteRaster(0, iY, srcDS.RasterXSize, 1, dstData,
                                                   srcDS.RasterXSize, 1, 0, 0);
                                 }
                             });
}
4

0 回答 0