在我的計劃,我需要寫大量的文本文件(〜300 MB),文本文件包含由空格分隔的數字,我使用這個代碼:增強性能
TextWriter guessesWriter = TextWriter.Synchronized(new StreamWriter("guesses.txt"));
private void QueueStart()
{
while (true)
{
if (writeQueue.Count > 0)
{
guessesWriter.WriteLine(writeQueue[0]);
writeQueue.Remove(writeQueue[0]);
}
}
}
private static void Check()
{
TextReader tr = new StreamReader("data.txt");
string guess = tr.ReadLine();
b = 0;
List<Thread> threads = new List<Thread>();
while (guess != null) // Reading each row and analyze it
{
string[] guessNumbers = guess.Split(' ');
List<int> numbers = new List<int>();
foreach (string s in guessNumbers) // Converting each guess to a list of numbers
numbers.Add(int.Parse(s));
threads.Add(new Thread(GuessCheck));
threads[b].Start(numbers);
b++;
guess = tr.ReadLine();
}
}
private static void GuessCheck(object listNums)
{
List<int> numbers = (List<int>) listNums;
if (!CloseNumbersCheck(numbers))
{
writeQueue.Add(numbers[0] + " " + numbers[1] + " " + numbers[2] + " " + numbers[3] + " " + numbers[4] + " " + numbers[5] + " " + numbers[6]);
}
}
private static bool CloseNumbersCheck(List<int> numbers)
{
int divideResult = numbers[0]/10;
for (int i = 1; i < 6; i++)
{
if (numbers[i]/10 != divideResult)
return false;
}
return true;
}
文件data.txt中包含的數據格式爲:(點意味着遵循同樣的邏輯更多的數字)
1 2 3 4 5 6 1
1 2 3 4 5 6 2
1 2 3 4 5 6 3
.
.
.
1 2 3 4 5 6 8
1 2 3 4 5 7 1
.
.
.
我知道這是不是很有效,我一直在尋找如何使人們更快一些建議。 如果你知道如何節省大量的數字比.txt更有效,我將不勝感激。
什麼樣的對象是writeQueue? – 2011-04-19 15:44:06
TextWriter.Synchronized()似乎無用,因爲您只能從一個線程(QueueStart())訪問文件。但是你的隊列或者列表「writeQueue」應該是同步的,如果不是的話。 – Kipotlov 2011-04-19 15:48:01
writeQueue是一個列表,我增加了緩衝區大小爲64K(感謝Gabe),我把它全部變成了1個線程(謝謝Jeffery),它確實做得更快。該程序sopused做削減關於彩票號碼,而不是pin碼... –
ofirl
2011-04-19 16:47:55