提高线程效率
本文关键字:效率 线程 高线程 | 更新日期: 2023-09-27 17:49:42
我有以下代码将读取文件的字节,然后构建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();
}
}
}
}
有没有办法提高这段代码的效率,让它运行得更快?
嗯,我可以看到两个紧迫的问题:
- 你睡懒觉了。为什么?
- 您正在使用字符串连接大量的次数。当创建长字符串时,将是糟糕的
假设您只是将输出写入控制台,那么最简单的方法是完全避免将整个文件读入内存。只需一次读取一个块(例如32K)并迭代每个字节,将每个字节的十六进制值直接写入控制台(我希望可以缓冲…)。
如果您必须在内存中构建字符串,请使用StringBuilder
-请参阅我关于该主题的文章以获得详细解释-但我真的认为您根本不应该这样做。
你的程序用8个线程处理144 MByte的数据,每个线程处理1个字节,然后休眠1ms。
因此,你的程序将花费至少5个小时睡觉…
我建议您删除Thread.Sleep
,并尝试是否可以提高您的程序的效率。