我对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个像素。外循环的一次迭代大约需要80ms才能执行。 这里最好的方法是什么? 创建多个线程并将我的缓冲区分成预定义的垃圾或使用并行类? 我将不胜感激任何形式的帮助。
谢谢。
这样工作吗? 'Data.imageBuffer [i + 1]'听起来像'IndexOutOfRangeException'给我。 –