C#.Net转换代码性能问题

时间:2013-10-09 09:11:29

标签: c# arrays performance matlab

我正在处理大文件,并且使用一些算法从MatLab转换为dll。其中之一是entropy.m。我将它转换为dll并在c#中使用它。如您所知,需要MWArray来执行计算。我将数据存储为段。将一个细分填充到MWNumericArray。计算段的熵然后清除MWNumericArray。但是这个过程太慢了。我该怎么做才能提高程序的性能? 这是我的代码:

            NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
            byte[] mainBuffer = new byte[wave.WaveFormat.AverageBytesPerSecond / 5];

            int read;
            int length = (int)wave.Length;
            List<MWArray> ent = new List<MWArray>();
            MWNumericArray arr1 = null;
            EntropyCalculation obj = new EntropyCalculation();
            if (file.Length % mainBuffer.Length != 0)
            {
                int value = (int)(wave.Length / mainBuffer.Length);
                length = mainBuffer.Length * value;
            }
            while (wave.Position != length)
            {
                List<double> segment = new List<double>();
                read = wave.Read(mainBuffer, 0, mainBuffer.Length);
                for (int i = 0; i < read / 8; i++)
                {
                    segment.Add((BitConverter.ToSingle(mainBuffer, i * 8)));
                }
                arr1 = segment.ToArray();
                MWArray result = obj.entropy(arr1);
                ent.Add(result);
                arr1 = null;
            }

1 个答案:

答案 0 :(得分:0)

也许你可以尝试下面的代码

while (wave.Position != length)
            {                
                UInt16 read = wave.Read(mainBuffer, 0, mainBuffer.Length);
                double[] segment = new double[read / 8];
                for (UInt16 i = 0; i < segment.Length; i++)
                {
                    segment[i] =BitConverter.ToSingle(mainBuffer, i * 8);
                }               
                ent.Add(obj.entropy(segment));                
            }