Я довольно новичок в C#, и мне нужна помощь в оптимизации моего кода.Оптимизация двойного цикла для скорости выполнения
struct Data { public static int numberOfPixels; public static string[] filePaths; public static string[] fileNames; public static double[] z; public static int numberOfFrames; public static byte[][] imageBuffer; public static int bufferSize = 1000; public static double[] num; public static double[] den; } public class Methods { public void RetrieveFileList() { Console.WriteLine("Please enter the folder path where all measurement files are stored: "); Data.filePaths = Directory.GetFiles(Console.ReadLine(),"*.bin"); Data.fileNames = new string[Data.filePaths.Length]; Data.numberOfFrames = Data.filePaths.Length; Data.z = new double[Data.filePaths.Length]; int n = 0; foreach(string file in Data.filePaths) { Data.fileNames[n] = Path.GetFileNameWithoutExtension(file); n++; } } public void CreatePositionArray() { Console.WriteLine("Please enter the stepsize used during the scan in nanometers: "); double stepsize = Convert.ToDouble(Console.ReadLine()); int n = 0; foreach(string file in Data.fileNames) { Data.z[n] = Convert.ToInt32(file) * stepsize/1000; n++; } } public void InitializeBufferArray() { Data.imageBuffer = new byte[Data.numberOfFrames][]; } public byte[] ReadBinaryFile(int index) { return File.ReadAllBytes(Data.filePaths[index]); ; } public void FillImageBuffer() { for (int i = 0; i < Data.bufferSize; i++) { Data.imageBuffer[i] = ReadBinaryFile(i); } Data.numberOfPixels = Data.imageBuffer[0].Length; Data.num = new double[Data.numberOfPixels]; Data.den = new double[Data.numberOfPixels]; } } class Program { static void Main(string[] args) { Method.RetrieveFileList(); Method.CreatePositionArray(); Method.InitializeBufferArray(); Method.FillImageBuffer(); for(int i = 0; i < Data.numberOfFrames; i++) { for (int j = 0; j < Data.numberOfPixels; j++) { double der = Math.Pow(Data.imageBuffer[i+1][j] - Data.imageBuffer[i][j], 2); if (der < 1) der = 0; Data.num[j] = Data.num[j] + Data.z[i] * der; Data.den[j] = Data.den[j] + der; } } } }
В частности, две петли в моем главном методе. Прямо сейчас этот цикл обрабатывает около 1000 кадров с 1210000 пикселей каждый. Одна итерация внешнего цикла занимает около 80 мс для выполнения. Что было бы лучшим способом здесь? Создайте несколько потоков и разделите мой буфер на предопределенные джонки или используйте класс Parallel? Буду признателен за любую помощь.
Спасибо.
Действительно ли это работает? 'Data.imageBuffer [i + 1]' звучит как 'IndexOutOfRangeException' для меня. –