我有一個工作很慢的我的任務功能(它必須是快10-100倍)如何使此代碼更快?
這裏是代碼
public long Support(List<string[]> sequences, string[] words)
{
var count = 0;
foreach (var sequence in sequences)
{
for (int i = 0; i < sequence.Length - words.Length + 1; i++)
{
bool foundSeq = true;
for (int j = 0; j < words.Length; j++)
{
foundSeq = foundSeq && sequence[i + j] == words[j];
}
if (foundSeq)
{
count++;
break;
}
}
}
return count;
}
public void Support(List<string[]> sequences, List<SequenceInfo> sequenceInfoCollection)
{
System.Threading.Tasks.Parallel.ForEach(sequenceInfoCollection.Where(x => x.Support==null),sequenceInfo =>
{
sequenceInfo.Support = Support(sequences, sequenceInfo.Sequence);
});
}
哪裏List<string[]> sequences
是文字的數組的數組。這個數組通常包含250k +行。每行約4-7個字。 string[] words
是我們嘗試計數的一組單詞(所有單詞至少包含一次)。
問題是foundSeq = foundSeq && sequence[i + j] == words[j];
。此代碼佔用了大部分執行時間(Enumerable.MoveNext位於第二位)。我想散列我的數組中的所有單詞。數字比字符串更快,對吧?我認爲它可以幫助我獲得30%-80%的表現。但我需要10倍!我能做什麼?如果你想知道它是apriory算法的一部分。
支持函數檢查單詞序列是否是序列列表中的任意序列的一部分並計數多少次。
請用相應的語言標籤進行標記。 – Mat
我建議您將您要解決的問題的描述移動到代碼上方。 –
@Hosam Aly只是序列中的字(字符串[]字)的計數(列表序列) –
Neir0