提高线程效率

时间:2012-12-27 11:42:09

标签: c# multithreading

我有以下代码,它将读取文件的字节,然后构造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();
            }

        }
    }
}

2 个答案:

答案 0 :(得分:7)

  

有没有提高此代码的效率,以便它可以更快地运行?

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

  • 你在你的循环中睡觉。为什么?
  • 您使用字符串连接巨大次。在创建长字符串时,这将是糟糕的

就个人而言,我建议你完全摆脱线程完全。这使得代码更加复杂,我怀疑当你修复上面的两个问题时,你可以使用单个线程。

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

如果您必须在内存中构建字符串,请使用StringBuilder - 请参阅my article on the topic以获取详细说明 - 但我真的认为您不应该这样做一点都不。

答案 1 :(得分:3)

您的程序处理144 MB的数据,其中8个线程处理1个字节,然后进入休眠状态1ms。

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

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