0

我有以下代码将读取文件的字节然后构造 8 个单独的字符串,一旦完成,8 个字符串将被拼凑在一起,但是当读取 500mb 文件时,此代码需要 10 多个小时,然后我放弃运行它.

有没有办法提高这段代码的效率,让它运行得更快?

namespace ConsoleApplication1
{
    public class object1
    {
        public static Byte[] split1 = new Byte[18082460];
        public static Byte[] split2 = new Byte[18082460];
        public static Byte[] split3 = new Byte[18082460];
        public static Byte[] split4 = new Byte[18082460];
        public static Byte[] split5 = new Byte[18082460];
        public static Byte[] split6 = new Byte[18082460];
        public static Byte[] split7 = new Byte[18082460];
        public static Byte[] split8 = new Byte[18082452];

        public static String[] output = new String[8];

        public void run1()
        {
            for (int i = 0; i < split1.Length; i++)
            {
                output[0] += "0x" + split1[i] + ", ";
                Program.amountDone[0] += 1;
                Thread.Sleep(1);
            }
        }
        public void run2()
        {
            for (int i = 0; i < split2.Length; i++)
            {
                output[1] += "0x" + split2[i] + ", ";
                Program.amountDone[1] += 1;
                Thread.Sleep(1);
            }
        }
        public void run3()
        {
            for (int i = 0; i < split3.Length; i++)
            {
                output[2] += "0x" + split3[i] + ", ";
                Program.amountDone[2] += 1;
                Thread.Sleep(1);
            }
        }
        public void run4()
        {
            for (int i = 0; i < split4.Length; i++)
            {
                output[3] += "0x" + split4[i] + ", ";
                Program.amountDone[3] += 1;
                Thread.Sleep(1);
            }
        }
        public void run5()
        {
            for (int i = 0; i < split5.Length; i++)
            {
                output[4] += "0x" + split5[i] + ", ";
                Program.amountDone[4] += 1;
                Thread.Sleep(1);
            }
        }
        public void run6()
        {
            for (int i = 0; i < split6.Length; i++)
            {
                output[5] += "0x" + split6[i] + ", ";
                Program.amountDone[5] += 1;
                Thread.Sleep(1);
            }
        }
        public void run7()
        {
            for (int i = 0; i < split7.Length; i++)
            {
                output[6] += "0x" + split7[i] + ", ";
                Program.amountDone[6] += 1;
                Thread.Sleep(1);
            }
        }
        public void run8()
        {
            for (int i = 0; i < split8.Length; i++)
            {
                if (i == split8.Length)
                {
                    output[7] += "0x" + split8[i];
                }
                else
                {
                    output[7] += "0x" + split8[i] + ", ";
                }
                Program.amountDone[7] += 1;
                Thread.Sleep(1);
            }
        }
    };
    class Program
    {
        public static int curPoint = 0;
        public static object1 obj = new object1();
        public static Thread thread1 = new Thread(new ThreadStart(obj.run1));
        public static Thread thread2 = new Thread(new ThreadStart(obj.run2));
        public static Thread thread3 = new Thread(new ThreadStart(obj.run3));
        public static Thread thread4 = new Thread(new ThreadStart(obj.run4));
        public static Thread thread5 = new Thread(new ThreadStart(obj.run5));
        public static Thread thread6 = new Thread(new ThreadStart(obj.run6));
        public static Thread thread7 = new Thread(new ThreadStart(obj.run7));
        public static Thread thread8 = new Thread(new ThreadStart(obj.run8));
        public static int[] amountDone = { 0, 0, 0, 0, 0, 0, 0, 0 };

        [STAThread]
        static void Main(string[] args)
        {
            Byte[] bytes = GetBytesFromFile(@"C:\Users\JLT\Desktop\320kbTest.rar");

            Array.Copy(bytes, 0, object1.split1, 0, 18082460);
            Array.Copy(bytes, 18082461, object1.split2, 0, 18082460);
            Array.Copy(bytes, 36164922, object1.split3, 0, 18082460);
            Array.Copy(bytes, 54247383, object1.split4, 0, 18082460);
            Array.Copy(bytes, 72329844, object1.split5, 0, 18082460);
            Array.Copy(bytes, 90412305, object1.split6, 0, 18082460);
            Array.Copy(bytes, 108494766, object1.split7, 0, 18082460);
            Array.Copy(bytes, 126577227, object1.split8, 0, 18082452);

            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
            thread5.Start();
            thread6.Start();
            thread7.Start();
            thread8.Start();


            String output = "Byte[] rawData = { ";

            while (true)
            {
                if (thread1.IsAlive || thread2.IsAlive || thread3.IsAlive || thread4.IsAlive || thread5.IsAlive || thread6.IsAlive || thread7.IsAlive || thread8.IsAlive)
                {
                    int temp = 0;
                    foreach (int inter in amountDone)
                    {
                        temp += inter;
                    }
                    Console.WriteLine(temp + " | " + bytes.Length);
                    Thread.Sleep(100);
                    Console.Clear();
                }
                else
                {
                    int temp = 0;
                    foreach (int inter in amountDone)
                    {
                        temp += inter;
                    }
                    if (temp < bytes.Length - 1)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            output += object1.output[0];
            output += object1.output[1];
            output += object1.output[2];
            output += object1.output[3];
            output += object1.output[4];
            output += object1.output[5];
            output += object1.output[6];
            output += object1.output[7];

            output += "};";
            Console.WriteLine(output);
        }

        public static byte[] GetBytesFromFile(string fullFilePath)
        {
            FileStream fs = File.OpenRead(fullFilePath);
            try
            {
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                return bytes;
            }
            finally
            {
                fs.Close();
            }

        }
    }
}
4

2 回答 2

7

有没有办法提高这段代码的效率,让它运行得更快?

好吧,我可以看到两个直接的问题:

  • 你在循环中睡觉。为什么?
  • 您正在大量使用字符串连接。创建长字符串时,这将是可怕的。

就个人而言,我建议您从一开始就完全摆脱线程。它使代码变得更加复杂,我怀疑当你解决了上面的两个问题后,你就可以使用一个线程了。

鉴于您只是将输出写入控制台,最简单的方法是完全避免将整个文件读入内存。只需一次读取一个块(例如 32K)并遍历每个字节,将每个字节的十六进制值直接写入控制台(我希望将其缓冲......)。

如果您必须在内存中构建字符串,请使用StringBuilder- 请参阅我关于该主题的文章以获得详细说明 - 但我真的认为您根本不应该这样做。

于 2012-12-27T11:48:34.977 回答
3

您的程序使用 8 个线程处理 144 MB 的数据,这些线程处理 1 个字节,然后休眠 1 毫秒。

因此,您的程序将花费至少 5 个小时来睡觉......

我建议您删除Thread.Sleep并尝试是否可以提高程序的效率。

于 2012-12-27T11:53:11.940 回答