我有一個字符串列表的列表,以便將字符串編號爲'1',如字符串=「00000」屬於第一組,字符串=「00001」屬於到第二組。 ALl字符串長度相等。現在我比較第一組和第二組以及第二組到第三組,並且很快...就像在圖像中一樣。第一組中的第一個元素與第二組中的所有元素進行比較。直到每個字符串進行比較。有沒有辦法加快我的程序的性能?所以我可以達到32000個長度爲15的字符串。嵌套迴路加速性能
編輯
對不起過去post.After讀它,我知道我是啞巴問這樣。該計劃的目標是一個簡化。基於該奎因 - 麥克拉斯基算法
考慮
000
001
010
011
100
101
110
111
我他們組由1
000
001
010
100
011
101
110
111
號碼,然後我每串比較從組到下一組
group 1
000
group 2
001
010
100
group 3
011
101
110
group1 -> group2
------------------
000 -> 001 = 00-
000 -> 010 = 0-0
000 -> 100 = -00
------------------
group2 ->group3
--------------------
001 -> 011 = 0-1
001 -> 101 = -01
001 -> 110 = no output
010 -> 011 = 01-
010 -> 101 = no output
010 -> 110 = -10
100 -> 011 = no output
100 -> 101 = 10-
100 -> 110 = 1-0
---------------------
etc.
然後再次按1的編號對輸出進行分組並再次進行比較,直到不能比較任何字符串。
我需要實現一個15變量,但它永遠爲程序完成。任何想法如何加速它。我在線程測試它,但只是一點點改進。
數量的字符串:變量的2048長度:11時間:10分鐘
極品取得
數量的字符串:變量的32767長度:15時間:無法實現
List<List<string>> ImplicantsByOneFinal = new List<List<string>>();
List<List<string>> TermsByOne = new List<List<string>>();
有沒有改進此代碼的方法或算法。它在11到15個變量上變慢。
bool CombineAndGroup(List<List<string>> ImplicantsByOne)
{
TermsByOne = new List<List<string>>();
int combined = 0;
for (int i = 0; i < ImplicantsByOne.Count - 1; i++)
{
List<string> termsGrouped = new List<string>();
for (int j = 0; j < ImplicantsByOne[i].Count; j++)
{
int combination = 0;
int num1 = Convert.ToInt32((ImplicantsByOne[i][j]).Replace('-','0'), 2);
for (int k = 0; k < ImplicantsByOne[i + 1].Count; k++)
{
int num2 = Convert.ToInt32((ImplicantsByOne[i + 1][k]).Replace('-', '0'), 2);
int num3 = num2 - num1;
double num4 = Math.Log((double)num3, (double)2);
if (((num4 % 1) == 0) && (num3 > 0) && (Esum(ImplicantsByOne[i][j]) == Esum(ImplicantsByOne[i + 1][k])))
{
string combinedMinterm = CompareString(ImplicantsByOne[i][j], ImplicantsByOne[i + 1][k]);
if (!termsGrouped.Contains(combinedMinterm))
{
termsGrouped.Add(combinedMinterm);
}
}
}
}
if (termsGrouped.Count > 0)
{
combined += termsGrouped.Count;
}
TermsByOne.Add(termsGrouped);
}
return (combined > 0) ? true : false;
}
private int Esum(String binCode)
{
binCode = binCode.Replace('1','0');
binCode = binCode.Replace('-', '1');
int esum = Convert.ToInt32(binCode, 2);
return esum;
}
//Purpose of CompareString is to compare two string and change the unique char to '-'
//like 000 and 001 = 00-
private string CompareString(string str1, string str2)
{
if (str1 == str2)
{
CountCompareStringLoops++;
return str1;
}
else
{
if (str1.Length == 1)
{
return "-";
}
int halflength = str1.Length/2;
return CompareString(str1.Substring(0, halflength), str2.Substring(0, halflength)) + CompareString(str1.Substring(halflength), str2.Substring(halflength));
}
}
主程序
MintermsByOne = Loaded with string 000 001 and so on
CombineAndGroup(MintermsByOne);
ImplicantsByOneFinal = TermsByOne;
while (CombineAndGroup(TermsByOne))
{
ImplicantsByOneFinal = TermsByOne;
}
輸出ImplicantsByOneFinal
我聞到一股XY問題。你真正想要完成的任務是什麼? – 2013-05-10 10:39:26
爲什麼你用C++和c標記了它,當它們都沒有顯示時?你爲什麼包含c#和c#-4.0和c#-3.0? – 2013-05-10 10:41:46
我喜歡這個形象,可能是我最喜歡的,當我想說明一個問題不清楚。 – 2013-05-10 10:50:07